How to map a triangle to a circle?

Started by
14 comments, last by nb 16 years, 1 month ago
Ok, I've made some progress!

I used the cos between O-P and the midperpendicular to get the base scale factor for the final point. Here is the code that maps the point correctly (I think) to the circle.

Vector2 MapPointOnEquilateralTriangleToCircle(Vector2 P){    float angle = Angle(new Vector2(0, 1), P);    angle *= 180.0f / (float)Math.PI;    if (angle < 0)        angle += 360.0f;    float cos;    if (angle <= 120)        cos = (float)Math.Cos(Angle(nrmAB, P));    else if (angle <= 240)        cos = (float)Math.Cos(Angle(nrmBC, P));    else        cos = (float)Math.Cos(Angle(nrmCA, P));    float f = 1 - (2 * (1 - cos));    Vector2 n = new Vector2(P);    float len = n.Normalize();    return P + (n * f * len);}

EDIT: removed code that did not work

[Edited by - Scythen on March 21, 2008 5:31:39 PM]
Advertisement
Ok, while mapping the point from the triangle to the circle is fine going the other way is not.

My test app is here test app if anyone cares to look.

Maybe some sleep will help [dead]
I think there's an easier / simpler / quicker way....
using just vector math, rather than trig, angles or y=mx+c ... ick!!

Normalise P into a Unit vector N
Then take the Dot product with a unit vector which is "normal" to one of the sides.
If the result is < 0.5, try again with a different side,
If the result is still < 0.5, the point must be "near" the 3rd side

I've tried to stick with your notation...
I've assumed the nrmAB, nrmBC and nrmCA are outward facing Unit Vectors..
and you've got Dot available.
Vector2 MapPointOnEquilateralTriangleToCircle(Vector2 P){   Vector2 n = new Vector2(P);    float len = n.Normalize();    float dp = n.Dot(nrmAB);    if (dp < 0.5) {            dp = n.Dot(nrmBC));       if (dp < 0.5) dp = n.Dot(nrmCA);    }    float f = dp * 2;// Optional - ensure output point is not outside circle//  if ( f*len > Radius ) f = radius / len;    return P * f; }


The Circle2Triangle routine is identical except, the last few lines
Vector2 MapPointOnCircletoEquilateralTriangle(Vector2 P){   Vector2 n = new Vector2(P);    float len = n.Normalize();    float dp = n.Dot(nrmAB);    if (dp < 0.5) {            dp = n.Dot(nrmBC));       if (dp < 0.5) dp = n.Dot(nrmCA);    }    float f = dp * 2;// Optional - ensure output point is not outside the triangle//  if ( len > Radius ) f = f * len / radius;    return P / f; }


HTH

[Edited by - daftasbrush on March 22, 2008 5:55:49 AM]
Ummm... Yea that helps!!!
At least I was close.

Thanks very much!


I didn't fully understand all the methods posted so I apologize if this has already been posted, but here's what I would do. Construct a ray from the center(o) to the point(p). Intersect that ray with the triangle(t)(3 line to line collisions) and the circle(c)(c=o+(p-o)*(r/|p-o|). If converting from the triangle to the to the circle the new point is n=o+(p-o)*|c-o|/|t-o| . If converting from the circle to the triangle the new point is n=o+(p-o)*|t-o|/|c-o|.
so much math for what sounds like a simple answer. a point around the circle mapped to a point around a triangle, and then the opposite??? it's all about ratios. If you are finding a point 2/5th's around the circle then just find the same point along the triangle sides. It's like a race track... side 1 then side 2 then side 3 takes you all the way back to the start, as does following the circumference of the circle. if you know how long each side of the triangle is then just figure-out how far along you've travelled around the circle and map that to the triangle.

in terms of points within the circle mapping to the triangle, then i'd be looking at what angle it is in the circle, and then taking into account how long each side is and figuring-out ...... no lets go with an example...

let's say that the triangle has sides 10 units long, 5 units long, and 5 units long again... obviously the 10 units long side takes-up 50% of the total distance 'around' the triangle track... therefore if the angle in the circle is within the first 50% of it's circumference then i'd be mapping how far in that 50% of the circle you are finding to the same point along the triangles first side of 10 units long... and then you just need to find the ratio between how far the point is to the circumference and map that to the distance from the triangle centre to the point you calculated along the first side just before.

This topic is closed to new replies.

Advertisement