MehemmedSettarli's blog

By MehemmedSettarli, history, 5 weeks ago, In English

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++.

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

A = (1/2) |x1(y2 − y3) + x2(y3 − y1) + x3(y1 − y2)|

A => Area of Triangle

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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)}$$$

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks a lot also i want to ask that how I can find the point lies on a segment or not?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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|$$$.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thank you very much

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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 for y 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)$$$.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yeah, you are right. Thank you for pointing out this error!