I have given 6 triangle indexes in 2D and I don't know how to calculate the area of this triangle example (1 1, 2 4, 3 2) are equal to 2.5. Can anyone help me, please? I am keen to write it in C++.
# | User | Rating |
---|---|---|
1 | jiangly | 3898 |
2 | tourist | 3840 |
3 | orzdevinwang | 3706 |
4 | ksun48 | 3691 |
5 | jqdai0815 | 3682 |
6 | ecnerwala | 3525 |
7 | gamegame | 3477 |
8 | Benq | 3468 |
9 | Ormlis | 3381 |
10 | maroonrk | 3379 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | -is-this-fft- | 165 |
3 | Dominater069 | 161 |
4 | atcoder_official | 159 |
4 | Um_nik | 159 |
6 | djm03178 | 156 |
7 | adamant | 153 |
8 | luogu_official | 150 |
9 | awoo | 149 |
10 | TheScrasse | 146 |
I have given 6 triangle indexes in 2D and I don't know how to calculate the area of this triangle example (1 1, 2 4, 3 2) are equal to 2.5. Can anyone help me, please? I am keen to write it in C++.
Name |
---|
A = (1/2) |x1(y2 − y3) + x2(y3 − y1) + x3(y1 − y2)|
A => Area of Triangle
this is called Shoelace formula, if someone wonders. https://en.wikipedia.org/wiki/Shoelace_formula
yeah it works for any polygon and the vertices should be in order either from 1st to last or opposite
yeah agreed. why is it called Shoelace formula tho? is it because of zig-zag pattern it makes when multiplying coordinates?
First, we can calculate the length of AB, AC, and BC using:
$$$AB = \sqrt{(B_x - A_x)^2 + (B_y - A_y)^2}$$$,
$$$AC = \sqrt{(C_x - A_x)^2 + (C_y - A_y)^2}$$$,
$$$BC = \sqrt{(C_x - B_x)^2 + (C_y - B_y)^2}$$$
Let's call $$$p = \frac{AB + AC + AD}{2}$$$
Area of triangle will $$$S = \sqrt{p\times(p-AB)\times(p-AC)\times(p-BC)}$$$
Thanks a lot also i want to ask that how I can find the point lies on a segment or not?
Let's say that you want to check whether the point $$$C$$$ lies on the segment $$$AB$$$. Point will lie on the segment if and only if area of triangle $$$ABC$$$ will be $$$0$$$ and $$$|A_x-C_x|+|C_x-B_x|=|A_x-B_x|$$$.
thank you very much
I don't think that checking
x
coordinates suffices: what if the segment $$$AB$$$ is vertical and $$$C$$$ is on the same line but outside $$$AB$$$ (in other words, $$$A_x = B_x = C_x$$$)? We have to check a similar equation fory
coordinates as well.Alternatively, after checking that the triangle $$$ABC$$$ has area of $$$0$$$, we can check if $$$\min(A_x, B_x) \leq C_x \leq \max(A_x, B_x)$$$ and $$$\min(A_y, B_y) \leq C_y \leq \max(A_y, B_y)$$$.
Yeah, you are right. Thank you for pointing out this error!