绘制OGIS定义的Polygon
public void DrawPolygon(Polygon pol, Brush brush, Pen pen, bool clip)
{
gc = Graphics.FromHwnd(Handle);
if (pol.ExteriorRing == null)
return;
if (pol.ExteriorRing.Vertices.Count > )
{
//Use a graphics path instead of DrawPolygon. DrawPolygon has a problem with several interior holes
GraphicsPath gp = new GraphicsPath(); //Add the exterior polygon
PointF[] pts = TransformLineString(pol.ExteriorRing);
if (!clip)
gp.AddPolygon(LimitValues(pts, extremeValueLimit));
else
DrawPolygonClipped(gp, pts, (int), (int)); //Add the interior polygons (holes) for (int i = ; i < pol.InteriorRings.Count; i++)
{
PointF[] pts1 = TransformLineString(pol.InteriorRings[i]);
if (!clip)
gp.AddPolygon(LimitValues(pts1, extremeValueLimit));
else
DrawPolygonClipped(gp, pts1, (int), (int));
} // Only render inside of polygon if the brush isn't null or isn't transparent
if (brush != null && brush != Brushes.Transparent)
gc.FillPath(brush, gp);
// Create an outline if a pen style is available
if (pen != null)
gc.DrawPath(pen, gp);
gc.Dispose();
}
}
public static PointF[] TransformLineString(LineString line)
{
PointF[] v = new PointF[line.Vertices.Count];
for (int i = ; i < line.Vertices.Count; i++)
v[i] = new PointF((float)line.Vertices[i].X, (float)line.Vertices[i].Y);
return v;
}
internal static PointF[] clipPolygon(PointF[] vertices, int width, int height)
{
float deltax, deltay, xin, xout, yin, yout;
float tinx, tiny, toutx, touty, tin1, tin2, tout;
float x1, y1, x2, y2; List<PointF> line = new List<PointF>();
if (vertices.Length <= ) /* nothing to clip */
return vertices; for (int i = ; i < vertices.Length - ; i++)
{
x1 = vertices[i].X;
y1 = vertices[i].Y;
x2 = vertices[i + ].X;
y2 = vertices[i + ].Y; deltax = x2 - x1;
if (deltax == )
{
// bump off of the vertical
deltax = (x1 > ) ? -nearZero : nearZero;
}
deltay = y2 - y1;
if (deltay == )
{
// bump off of the horizontal
deltay = (y1 > ) ? -nearZero : nearZero;
} if (deltax > )
{
// points to right
xin = ;
xout = width;
}
else
{
xin = width;
xout = ;
} if (deltay > )
{
// points up
yin = ;
yout = height;
}
else
{
yin = height;
yout = ;
} tinx = (xin - x1) / deltax;
tiny = (yin - y1) / deltay; if (tinx < tiny)
{
// hits x first
tin1 = tinx;
tin2 = tiny;
}
else
{
// hits y first
tin1 = tiny;
tin2 = tinx;
} if ( >= tin1)
{
if ( < tin1)
line.Add(new PointF(xin, yin)); if ( >= tin2)
{
toutx = (xout - x1) / deltax;
touty = (yout - y1) / deltay; tout = (toutx < touty) ? toutx : touty; if ( < tin2 || < tout)
{
if (tin2 <= tout)
{
if ( < tin2)
{
if (tinx > tiny)
line.Add(new PointF(xin, y1 + tinx * deltay));
else
line.Add(new PointF(x1 + tiny * deltax, yin));
} if ( > tout)
{
if (toutx < touty)
line.Add(new PointF(xout, y1 + toutx * deltay));
else
line.Add(new PointF(x1 + touty * deltax, yout));
}
else
line.Add(new PointF(x2, y2));
}
else
{
if (tinx > tiny)
line.Add(new PointF(xin, yout));
else
line.Add(new PointF(xout, yin));
}
}
}
}
}
if (line.Count > )
line.Add(new PointF(line[].X, line[].Y)); return line.ToArray();
} private void DrawPolygonClipped(GraphicsPath gp, PointF[] polygon, int width, int height)
{
ClipState clipState = DetermineClipState(polygon, width, height);
if (clipState == ClipState.Within)
{
gp.AddPolygon(polygon);
}
else if (clipState == ClipState.Intersecting)
{
PointF[] clippedPolygon = clipPolygon(polygon, width, height);
if (clippedPolygon.Length > )
gp.AddPolygon(clippedPolygon);
}
}