zoukankan      html  css  js  c++  java
  • [RGEOS]绘制多边形Polygon

     绘制OGIS定义的Polygon

     1    public void DrawPolygon(Polygon pol, Brush brush, Pen pen, bool clip)
     2         {
     3             gc = Graphics.FromHwnd(Handle);
     4             if (pol.ExteriorRing == null)
     5                 return;
     6             if (pol.ExteriorRing.Vertices.Count > 2)
     7             {
     8                 //Use a graphics path instead of DrawPolygon. DrawPolygon has a problem with several interior holes
     9                 GraphicsPath gp = new GraphicsPath();
    10 
    11                 //Add the exterior polygon
    12                 PointF[] pts = TransformLineString(pol.ExteriorRing);
    13                 if (!clip)
    14                     gp.AddPolygon(LimitValues(pts, extremeValueLimit));
    15                 else
    16                     DrawPolygonClipped(gp, pts, (int)100, (int)100);
    17 
    18                 //Add the interior polygons (holes)
    19 
    20                 for (int i = 0; i < pol.InteriorRings.Count; i++)
    21                 {
    22                     PointF[] pts1 = TransformLineString(pol.InteriorRings[i]);
    23                     if (!clip)
    24                         gp.AddPolygon(LimitValues(pts1, extremeValueLimit));
    25                     else
    26                         DrawPolygonClipped(gp, pts1, (int)100, (int)100);
    27                 }
    28 
    29                 // Only render inside of polygon if the brush isn't null or isn't transparent
    30                 if (brush != null && brush != Brushes.Transparent)
    31                     gc.FillPath(brush, gp);
    32                 // Create an outline if a pen style is available
    33                 if (pen != null)
    34                     gc.DrawPath(pen, gp);
    35                 gc.Dispose();
    36             }
    37         }
    1   public static PointF[] TransformLineString(LineString line)
    2         {
    3             PointF[] v = new PointF[line.Vertices.Count];
    4             for (int i = 0; i < line.Vertices.Count; i++)
    5                 v[i] = new PointF((float)line.Vertices[i].X, (float)line.Vertices[i].Y);
    6             return v;
    7         }
      1         internal static PointF[] clipPolygon(PointF[] vertices, int width, int height)
      2         {
      3             float deltax, deltay, xin, xout, yin, yout;
      4             float tinx, tiny, toutx, touty, tin1, tin2, tout;
      5             float x1, y1, x2, y2;
      6 
      7             List<PointF> line = new List<PointF>();
      8             if (vertices.Length <= 1) /* nothing to clip */
      9                 return vertices;
     10 
     11             for (int i = 0; i < vertices.Length - 1; i++)
     12             {
     13                 x1 = vertices[i].X;
     14                 y1 = vertices[i].Y;
     15                 x2 = vertices[i + 1].X;
     16                 y2 = vertices[i + 1].Y;
     17 
     18                 deltax = x2 - x1;
     19                 if (deltax == 0)
     20                 {
     21                     // bump off of the vertical
     22                     deltax = (x1 > 0) ? -nearZero : nearZero;
     23                 }
     24                 deltay = y2 - y1;
     25                 if (deltay == 0)
     26                 {
     27                     // bump off of the horizontal
     28                     deltay = (y1 > 0) ? -nearZero : nearZero;
     29                 }
     30 
     31                 if (deltax > 0)
     32                 {
     33                     //  points to right
     34                     xin = 0;
     35                     xout = width;
     36                 }
     37                 else
     38                 {
     39                     xin = width;
     40                     xout = 0;
     41                 }
     42 
     43                 if (deltay > 0)
     44                 {
     45                     //  points up
     46                     yin = 0;
     47                     yout = height;
     48                 }
     49                 else
     50                 {
     51                     yin = height;
     52                     yout = 0;
     53                 }
     54 
     55                 tinx = (xin - x1) / deltax;
     56                 tiny = (yin - y1) / deltay;
     57 
     58                 if (tinx < tiny)
     59                 {
     60                     // hits x first
     61                     tin1 = tinx;
     62                     tin2 = tiny;
     63                 }
     64                 else
     65                 {
     66                     // hits y first
     67                     tin1 = tiny;
     68                     tin2 = tinx;
     69                 }
     70 
     71                 if (1 >= tin1)
     72                 {
     73                     if (0 < tin1)
     74                         line.Add(new PointF(xin, yin));
     75 
     76                     if (1 >= tin2)
     77                     {
     78                         toutx = (xout - x1) / deltax;
     79                         touty = (yout - y1) / deltay;
     80 
     81                         tout = (toutx < touty) ? toutx : touty;
     82 
     83                         if (0 < tin2 || 0 < tout)
     84                         {
     85                             if (tin2 <= tout)
     86                             {
     87                                 if (0 < tin2)
     88                                 {
     89                                     if (tinx > tiny)
     90                                         line.Add(new PointF(xin, y1 + tinx * deltay));
     91                                     else
     92                                         line.Add(new PointF(x1 + tiny * deltax, yin));
     93                                 }
     94 
     95                                 if (1 > tout)
     96                                 {
     97                                     if (toutx < touty)
     98                                         line.Add(new PointF(xout, y1 + toutx * deltay));
     99                                     else
    100                                         line.Add(new PointF(x1 + touty * deltax, yout));
    101                                 }
    102                                 else
    103                                     line.Add(new PointF(x2, y2));
    104                             }
    105                             else
    106                             {
    107                                 if (tinx > tiny)
    108                                     line.Add(new PointF(xin, yout));
    109                                 else
    110                                     line.Add(new PointF(xout, yin));
    111                             }
    112                         }
    113                     }
    114                 }
    115             }
    116             if (line.Count > 0)
    117                 line.Add(new PointF(line[0].X, line[0].Y));
    118 
    119             return line.ToArray();
    120         }
    121 
    122         private void DrawPolygonClipped(GraphicsPath gp, PointF[] polygon, int width, int height)
    123         {
    124             ClipState clipState = DetermineClipState(polygon, width, height);
    125             if (clipState == ClipState.Within)
    126             {
    127                 gp.AddPolygon(polygon);
    128             }
    129             else if (clipState == ClipState.Intersecting)
    130             {
    131                 PointF[] clippedPolygon = clipPolygon(polygon, width, height);
    132                 if (clippedPolygon.Length > 2)
    133                     gp.AddPolygon(clippedPolygon);
    134             }
    135         }
  • 相关阅读:
    【C#进阶系列】06 类型和成员基础
    纪中5日T1 1564. 旅游
    纪中17日T1 2321. 方程
    纪中17日T2 2322. capacitor
    纪中10日T1 2313. 动态仙人掌
    纪中14日听课小结 图论 最短路 二分图 差分约束
    一个抓猫的游戏 消遣GAME 持续更新中!
    洛谷P1464 Function  HDU P1579 Function Run Fun
    洛谷P1976 鸡蛋饼
    纪中12日T1 2307. 选择
  • 原文地址:https://www.cnblogs.com/yhlx125/p/3650993.html
Copyright © 2011-2022 走看看