7

I'm trying to draw lines with offset to main line like on attachment. enter image description here

I have problems with my code. It generating intersections and cusps on the lines. (attachment)

enter image description here

Maybe someone can help me with this code provide any working example that I can follow.

// LEFT SIDE OF MAIN LINE
     int numberOfLines = 10;
     float offset = 10f;
     lastLinePoints = outerPoints; // outerPoint = Points from Main Line
     for(int i = 0; i < numberOfLines; i++)
     {
         List<Vector3> tempPoints = new List<Vector3> ();
         for (int k = 0; k < lastLinePoints.Count; k++) {
             if (k + 1 < lastLinePoints.Count) {
                 Vector3 direction = lastLinePoints [k + 1] - lastLinePoints [k];
                 // up direction:
                 Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);
                 // find right vector:
                 Vector3 right =  Vector3.Cross(direction.normalized, up.normalized);
                 Vector3 newPoint = lastLinePoints [k] + (right * offset);
                 tempPoints.Add (newPoint);
             }


         }
         VectorLine lineTemp = new VectorLine ("lineCurved", tempPoints, 120f / _camera2DObject.GetComponent<Camera> ().orthographicSize, LineType.Continuous);
         lineTemp.Draw3D ();
         lastLinePoints = tempPoints;

     }

After some research I know that solution for drawing curved parallel lines can be difficult. I found also some algorithms (https://hal.inria.fr/inria-00518005/document) but this mathematics is to hard for me to make code from it.

After suggestion from @jstreet I tried CLIPPER library. Results are very good but is it possible to draw only parallel line instead closed polygon around line (like on attachment) enter image description here

UPDATE

I wrote another question becouse I think that using CLIPPER for parallel lines is worth it. LINK TO question

enter image description here

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
seek
  • 1,065
  • 16
  • 33
  • Can you provide a complete example of what you do? So that it actually draws lines from your attached image. – Evk Apr 04 '16 at 19:30
  • This code actually draws these lines except the first one. First one line points are in outerPoints List. I think it is unnecessary to attach here List with hundreds of points – seek Apr 05 '16 at 11:27
  • And what library do you use for drawing? – Evk Apr 05 '16 at 11:28
  • Vectrosity but it isn't related with library. It is problem of all lines with offset – seek Apr 05 '16 at 11:28
  • yes I understand the problem. Just to have a starting point with your code. – Evk Apr 05 '16 at 11:30
  • Understand. I'm trying to write code now that can find intersection on line and remove segments putting intersection point instead but for now without any success. – seek Apr 05 '16 at 11:39
  • 1
    Take a look at [CLIPPER](http://www.angusj.com/delphi/clipper.php#screenshots). – jsanalytics Apr 05 '16 at 11:52
  • @jstreet I looked at CLIPPER but, clipper doesnt have any method for open line offseting, it creates only polygon from line. And if I'm not wrong it works only on IntPoints? – seek Apr 05 '16 at 11:53
  • From their documentation: _The Clipper Library performs clipping, and **offsetting** of both **lines** and polygons._ Also the documentation has some explanation about the geometry involved and links to papers. Not to mention the source code. – jsanalytics Apr 05 '16 at 12:16
  • ok @jstreet I will take a look at this again – seek Apr 05 '16 at 12:16
  • @jstreet I just edit my question after trying CLIPPER library. Do you is it possible to draw only parallel lines instead polygon around main line? – seek Apr 05 '16 at 15:21
  • Look at enum `EndType`. It will give you one open end if you use, for instance, `etOpenRound`, but unfortunately not both AFAIK.... For that you would probably need to loop over the solution point collection and, for instance, use only about "half" of that. A bit of a hack but could work. – jsanalytics Apr 05 '16 at 15:36
  • @jstreet I'm using etOpenRound at the example in attachment. I will look to make some hack for this as you suggest. – seek Apr 05 '16 at 15:39
  • 1
    @seek see related **QA**'s [draw outline for some connected lines](http://stackoverflow.com/a/22068534/2521214) and [How can I create an internal spiral for a polygon?](http://stackoverflow.com/a/31013424/2521214) – Spektre Apr 08 '16 at 07:40

2 Answers2

0

From My Previous Experience, lot of time will be spent to solve your problem without applying a polyline curves offset algorithm,so my advice is to start implementing any of the algorithms regardless the mathematical difficulties. choose one of the published algorithm that suits exactly your case, it could be easier than implementing an algorithm for any shape. But you can get the below link a shot https://github.com/skyrpex/clipper

Hussein Khalil
  • 1,395
  • 11
  • 29
  • 1
    Please read my updated question. I'm using CLIPPER but without any good results. Clipper can't make offset around path but can't do simple parallel line and I have no idea how to modify clipper to make this :/ – seek Apr 21 '16 at 15:02
0
  1. calculate parallel line parameters: you will need to calculate offset as coefficient (angle) remains the same.
  2. calculate line intersections between neighbouring lines based on calculated values from step 1.
  3. use splines over sequential sets of three intersection points. For splines you can use any cubic spline library, there are plenty in guthub (https://gist.github.com/dreikanter/3526685) or codeproject (http://www.codeproject.com/Articles/560163/Csharp-Cubic-Spline-Interpolation).
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
  • 1
    Please read my updated question. I'm using CLIPPER but without any good results. Clipper can't make offset around path but can't do simple parallel line and I have no idea how to modify clipper to make this :/ – seek Apr 21 '16 at 15:02
  • Updated to avoid using CLIPPER. – Riad Baghbanli Apr 21 '16 at 15:21