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 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | nor | 150 |
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
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!