geometrygodotareagodot4

Is there a problem with calculating triangle area using two vectors instead of three points?


I looked up calculating the area of triangle given three vertices.

func calculate_area_of_triangle(a: Vector2, b: Vector2, c: Vector2) -> float:
    return abs((a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) * 0.5)

func main():
    var points: Array[Vector2] = [Vector2(4.0, 10.0), Vector2(3.0, 7.0), Vector2(8.0, 0.0)]

        calculate_area_of_triangle(points[0], points[1], points[2])

Is there a problem rewriting it like this, using two vectors from a common point?

func calculate_area_of_triangle(a: Vector2, b: Vector2) -> float:
    return abs(a.x*b.y - b.x*a.y)*0.5

func main():
    var points: Array[Vector2] = [Vector2(4.0, 10.0), Vector2(3.0, 7.0), Vector2(8.0, 0.0)]

        calculate_area_of_triangle(points[0]-points[2], points[1]-points[2])

From my tests I haven’t found an issue, but I worry there is something I’m not thinking about.

(I’m using this for calculating an area of polygon. This is for a calculating the area of non-hollow polygon. I take the vertices and subtract the center from them.)


Solution

  • There are no problems. You just calculate area of the same triangle being translated to put one vertex into coordinate origin. Result is correct

    enter image description here