2013-12-13 12 views
0

Clipper 라이브러리를 사용하여 그래픽 경로를 수정하려고합니다.Clipper 라이브러리를 사용하여 경로 확대 및 채우기 방법

나는 윤곽선/획을 나타내는 너비 목록이 있습니다. 나는 가장 큰 것부터 시작하여 가장 작은 것으로 나아가고 싶다.

이 예를 들어, 우리는 20 (10) 내 그래픽 경로을 먹고 싶어

의 폭이 2 스트로크를 추가합니다, 새로운 그래픽 경로로 20 개 픽셀하여 오프셋/확장합니다. 원래 경로를 변경하고 싶지 않습니다. 그런 다음 새로운 그래픽 경로를 단색으로 채 웁니다.

다음으로 원본 그래픽 경로를 가져 와서 10 픽셀 씩 새 그래픽 경로로 확장/오프셋합니다. 나는이 새로운 길을 다른 색으로 채우고 싶다.

그런 다음 원본 경로를 다른 색으로 채 웁니다.

이 작업을 수행하는 올바른 방법은 무엇입니까? 나는이 작업을 시도하기 위해 만든 다음과 같은 방법을 사용하지만 제대로 작동하지 않습니다.

private void createImage(Graphics g, GraphicsPath gp, List<int> strokeWidths) 
{ 
    ClipperOffset pathConverter = new ClipperOffset(); 
    Clipper c = new Clipper(); 
    gp.Flatten(); 

    foreach(int strokeSize in strokeWidths) 
    { 
    g.clear(); 
    ClipperPolygons polyList = new ClipperPolygons(); 
    GraphicsPath gpTest = (GraphicsPath)gp.Clone();  
    PathToPolygon(gpTest, polyList, 100); 
    gpTest.Reset(); 

    c.Execute(ClipType.ctUnion, polyList, PolyFillType.pftPositive, PolyFillType.pftEvenOdd); 
    pathConverter.AddPaths(polyList, JoinType.jtMiter, EndType.etClosedPolygon);     
    pathConverter.Execute(ref polyList, strokeSize * 100); 

    for (int i = 0; i < polyList.Count; i++) 
    { 
     // reverses scaling 
     PointF[] pts2 = PolygonToPointFArray(polyList[i], 100); 
     gpTest.AddPolygon(pts2); 
    } 
    g.FillPath(new SolidBrush(Color.Red), gpTest); 
    } 
} 

     private void PathToPolygon(GraphicsPath path, ClipperPolygons polys, Single scale) 
     { 
      GraphicsPathIterator pathIterator = new GraphicsPathIterator(path); 
      pathIterator.Rewind(); 
      polys.Clear(); 
      PointF[] points = new PointF[pathIterator.Count]; 
      byte[] types = new byte[pathIterator.Count]; 
      pathIterator.Enumerate(ref points, ref types); 
      int i = 0; 
      while (i < pathIterator.Count) 
      { 
       ClipperPolygon pg = new ClipperPolygon(); 
       polys.Add(pg); 
       do 
       { 

        IntPoint pt = new IntPoint((int)(points[i].X * scale), (int)(points[i].Y * scale)); 
        pg.Add(pt); 
        i++; 
       } 
       while (i < pathIterator.Count && types[i] != 0); 
      } 
     } 
     private PointF[] PolygonToPointFArray(ClipperPolygon pg, float scale) 
     { 
      PointF[] result = new PointF[pg.Count]; 
      for (int i = 0; i < pg.Count; ++i) 
      { 
       result[i].X = (float)pg[i].X/scale; 
       result[i].Y = (float)pg[i].Y/scale; 
      } 
      return result; 
     } 

답변

0

매우 합리적인 출발을 한 동안 createImage() 함수에서 혼란스러워지고있는 것처럼 보입니다. 다른 오프셋을 사용하여 다른 색상을 원한다는 것을 언급하고 있으므로 strokeWidths 배열과 일치하는 색상 배열이 누락되었습니다. 또한 클리핑 (노조)으로 무엇을하고 있는지 불분명하지만 아마 불필요합니다.

그래서 의사 코드에서 나는 다음과 같은 ....

static bool CreateImage(Graphics g, GraphicsPath gp, 
    List<int> offsets, List<Color> colors) 
{ 
    const scale = 100; 
    if (colors.Count < offsets.Count) return false; 

    //convert GraphicsPath path to Clipper paths ... 
    Clipper.Paths cpaths = GPathToCPaths(gp.Flatten(), scale); 

    //setup the ClipperOffset object ... 
    ClipperOffset co = new ClipperOffsets(); 
    co.AddPaths(cpaths, JoinType.jtMiter, EndType.etClosedPolygon); 

    //now loop through each offset ... 
    foreach(offset in offsets, color in colors) 
    { 
     Clipper.Paths csolution = new Clipper.Paths(); 
     co.Execute(csolution, offset); 
     if (csolution.IsEmpty) break; //useful for negative offsets 
     //now convert back to floating point coordinate array ... 
     PointF[] solution = CPathToPointFArray(csolution, scale); 
     DrawMyPaths(Graphics g, solution, color); 
    } 
} 

그리고 무언가를보고에 대한 당신이 점점 더 큰 오프셋을 사용했다 경우 'foreach는'그려 각 다각형과 같은 제안 루프는 이전에 그린 폴리곤을 숨 깁니다.