How to know which hatch/CurvePolygon an entity belong to?
Posted: 11 Jul 2016, 14:24
Hi Mikhail,
How to know which hatch/CurvePolygon an entity belong to (Covering to)?
I try loop using entity.box.center and CurvePolygon.boundaries properties but it's seem they each have difffrent coordinate measurement. Although in the PictureBox the entity is inside the Hatch/CurvePolygon but the coordinate is different
I download the PointInPolygon from another source in the web, it's should be right.
some help please
Thank in advance
Sen
How to know which hatch/CurvePolygon an entity belong to (Covering to)?
I try loop using entity.box.center and CurvePolygon.boundaries properties but it's seem they each have difffrent coordinate measurement. Although in the PictureBox the entity is inside the Hatch/CurvePolygon but the coordinate is different
Code: Select all
//eAREA_List = all CurvePolygon entities in cadImage
//cUnit = entity text need to test inside/outside the CurvePolygon
foreach (var item in eAREA_List)
{
bool lInside = false;
if (item.ThisHatch_CP.Boundaries.Count > 0)
{
for (int i = 0; i < item.ThisHatch_CP.Boundaries.Count; i++)
{
PointF[] b1 = new PointF[item.ThisHatch_CP.Boundaries[i].Count + 1];
for (int j = 0; j < item.ThisHatch_CP.Boundaries[i].Count; j++)
{
DPoint rp = GetRealPointNode(new DPoint(item.ThisHatch_CP.Boundaries[i][j].X, item.ThisHatch_CP.Boundaries[i][j].Y, 0));
float X = float.Parse(rp.X.ToString());
float Y = float.Parse(rp.Y.ToString());
b1[j] = new PointF(X, Y);
}
//add close circle
DPoint rp0 = GetRealPointNode(new DPoint(item.ThisHatch_CP.Boundaries[i][0].X, item.ThisHatch_CP.Boundaries[i][0].Y, 0));
float XX = float.Parse(rp0.X.ToString());
float YY = float.Parse(rp0.Y.ToString());
b1[item.ThisHatch_CP.Boundaries[i].Count] = new PointF(XX, YY);
if (PointInPolygon(b1, float.Parse(item.CenterPointX.ToString()), float.Parse(item.CenterPointY.ToString())))
{
lInside = true;
break;
}
}
Code: Select all
// Return True if the point is in the polygon.
#region IS INSIDE POLYGON
public bool PointInPolygon(PointF[] Points, float X, float Y)
{
// Get the angle between the point and the
// first and last vertices.
int max_point = Points.Length - 1;
float total_angle = GetAngle(
Points[max_point].X, Points[max_point].Y,
X, Y,
Points[0].X, Points[0].Y);
// Add the angles from the point
// to each other pair of vertices.
for (int i = 0; i < max_point; i++)
{
total_angle += GetAngle(
Points[i].X, Points[i].Y,
X, Y,
Points[i + 1].X, Points[i + 1].Y);
}
// The total angle should be 2 * PI or -2 * PI if
// the point is in the polygon and close to zero
// if the point is outside the polygon.
return (Math.Abs(total_angle) > 0.000001);
}
public static float GetAngle(float Ax, float Ay,
float Bx, float By, float Cx, float Cy)
{
// Get the dot product.
float dot_product = DotProduct(Ax, Ay, Bx, By, Cx, Cy);
// Get the cross product.
float cross_product = CrossProductLength(Ax, Ay, Bx, By, Cx, Cy);
// Calculate the angle.
return (float)Math.Atan2(cross_product, dot_product);
}
private static float DotProduct(float Ax, float Ay,
float Bx, float By, float Cx, float Cy)
{
// Get the vectors' coordinates.
float BAx = Ax - Bx;
float BAy = Ay - By;
float BCx = Cx - Bx;
float BCy = Cy - By;
// Calculate the dot product.
return (BAx * BCx + BAy * BCy);
}
public static float CrossProductLength(float Ax, float Ay,
float Bx, float By, float Cx, float Cy)
{
// Get the vectors' coordinates.
float BAx = Ax - Bx;
float BAy = Ay - By;
float BCx = Cx - Bx;
float BCy = Cy - By;
// Calculate the Z coordinate of the cross product.
return (BAx * BCy - BAy * BCx);
}
some help please
Thank in advance
Sen