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++.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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++.
Название |
---|
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!