[Help] Areas Between Two Polyline

Need help, or want to share a macro? Post here!
HakanSeven12
Posts: 1065
Joined: Wed Feb 06, 2019 10:30 pm

[Help] Areas Between Two Polyline

I have two 2D point list that represent polylines that showed in pic and I want to calculate areas between this polylines like showed in picture(Sum orange areas and sum green areas). İn orange areas blue line over red line and in green areas red line over blue line. I have lots of this type polylines. So I want to automate this process. Is there any idea how can I do that?
Attachments
area.png (68.66 KiB) Viewed 447 times
Last edited by HakanSeven12 on Sat Nov 28, 2020 6:03 pm, edited 1 time in total.
johnwang
Posts: 578
Joined: Sun Jan 27, 2019 12:41 am

Re: [Help] Areas Between Two Polyline

1 Make a list of intersection points
2 calculate between each pair of intersection points for both polylines
3 Sblue-Sred
4 Sum up (+ and -)
Last edited by johnwang on Thu Nov 26, 2020 12:47 am, edited 3 times in total.
HakanSeven12
Posts: 1065
Joined: Wed Feb 06, 2019 10:30 pm

Re: [Help] Areas Between Two Polyline

How can I get the intersection points? How can I project start-end points to other line?
Roy_043
Posts: 2359
Joined: Thu Dec 27, 2018 12:28 pm

Re: [Help] Areas Between Two Polyline

The Draft module has a findIntersection function.
heda
Posts: 145
Joined: Sat Dec 12, 2015 5:49 pm

Re: [Help] Areas Between Two Polyline

If using findIntersection in DraftGeomUtils, it works with edges, so you will have to use those to get the intersections.
Just create a new vertical line at the ends and use the findIntersection on the vertical lines
I'm guessing it will be more code than one first expects to make it work on different cases.

btw - let us know if you managed to get it done.
HakanSeven12
Posts: 1065
Joined: Wed Feb 06, 2019 10:30 pm

Re: [Help] Areas Between Two Polyline

The problem is I just have two polyline x,y coordinates only. I draw this just show what I want. So I can't use draft functions. If I find a way I will inform you

But your idea is good. I don't think it that way.

Edit: I found this intersection algorithm on internet.

Code: Select all

``````def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

def det(a, b):
return a[0] * b[1] - a[1] * b[0]

div = det(xdiff, ydiff)
if div == 0:
return None

d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y

def poly_intersection(poly1, poly2):

for i, p1_first_point in enumerate(poly1[:-1]):
p1_second_point = poly1[i + 1]

for j, p2_first_point in enumerate(poly2[:-1]):
p2_second_point = poly2[j + 1]

if line_intersection((p1_first_point, p1_second_point), (p2_first_point, p2_second_point)):
return True

return False

PL1 = ((-1, -1), (1, -1), (1, 2))
PL2 = ((0, 1), (2, 1))

print poly_intersection(PL1, PL2)``````
heda
Posts: 145
Joined: Sat Dec 12, 2015 5:49 pm

Re: [Help] Areas Between Two Polyline

Well, so you just have the coords, like text?
If so, not entirely clear in your question - and was going to say why do that in freecad?
Sounds like fairly easy geometry to be done in python directly (or any other language, or spreadsheet application for that matter).
Seems like you have come to that conclusion of your own.
Now, even if you just have points you could do that in fc, but I would use numpy for that (and matplotlib to verify results)....
HakanSeven12
Posts: 1065
Joined: Wed Feb 06, 2019 10:30 pm

Re: [Help] Areas Between Two Polyline

Its about my workflow. Me and a friend creating a new workbench and I need to calculate volume between two surface by draw it's cross sections and calculate those areas and create volume tables and process goes like this. I can't use standard FreeCAD functions and objects because my coordinates are double values. So I'm using specific node in coin 3d. So I need to write nearly everything from zero.
Roy_043
Posts: 2359
Joined: Thu Dec 27, 2018 12:28 pm

Re: [Help] Areas Between Two Polyline

The findIntersection also accepts 4 vertices:
You can also feed 4 points instead of `edge1` and `edge2`.
HakanSeven12
Posts: 1065
Joined: Wed Feb 06, 2019 10:30 pm

Re: [Help] Areas Between Two Polyline

I will take a look. Thanks