Finding center of 2D triangle?

2020-02-05 02:30发布

I've been given a struct for a 2D triangle with x and y coordinates, a rotation variable, and so on. From the point created by those x and y coordinates, I am supposed to draw a triangle around the point and rotate it appropriately using the rotation variable.

I'm familiar with drawing triangles in OpenGl with GL_TRIANGLES. My problem is somehow extracting the middle of a triangle and drawing the vertices around it.

edit: Yes, what I am looking for is the centroid.

4条回答
2楼-- · 2020-02-05 02:36

By "middle" do you mean "centroid", a.k.a. the center of gravity if it were a 3D object of constant thickness and density?

If so, then pick two points, and find the midpoint between them. Then take this midpoint and the third point, and find the point 1/3 of the way between them (closer to the midpoint). That's your centroid. I'm not doing the math for you.

查看更多
我只想做你的唯一
3楼-- · 2020-02-05 02:43

There are different "types" of centers of a triangle. Details on: The Centers of a Triangle. A quick method for finding a center of a triangle is to average all your point's coordinates. For example:

GLfloat centerX = (tri[0].x + tri[1].x + tri[2].x) / 3;
GLfloat centerY = (tri[0].y + tri[1].y + tri[2].y) / 3;

When you find the center, you will need to rotate your triangle about the center. To do this, translate so that the center is now at (0, 0). Perform your rotation. Now reverse the translation you performed earlier.

查看更多
疯言疯语
4楼-- · 2020-02-05 02:45

I guess you mean the centroid of the triangle!?
This can be easily computed by 1/3(A + B + C) where A, B and C are the respective points of the triangle. If you have your points, you can simply multiply them by your rotation matrix as usual. Hope i got you right.

查看更多
你好瞎i
5楼-- · 2020-02-05 02:58

There are several points in a triangle that can be considered to be its center (orthocenter, centroid, etc.). This section of the Wikipedia article on triangles has more information. Just look at the pictures to get a quick overview.

查看更多
登录 后发表回答