0

I'm having some problems appending two lists programatically. I want to find whether a list of points are laying on an edge or inside the polygon (they can't lay outside).What I have is:

  1. 'pedges' is a list of edges forming a closed polygon in Abaqus

  2. 'areas' is a list of 'floats'

  3. There is an in-built function called getDistance that I am sure works well (from Abaqus)

    a) If a point lays on the edge then the area at this position is added to areas_onEdge=[]

    b) If a point doesn't lay on the edge then the area at this position is added to areas_inside=[]

  4. Finally I calculate sum(areas_inside)/areaShell

I have tried some straight forward code but it doesn't work.

What I am doing wrong?

areaShell=368.97;
areas_onEdge=[]
areas_inside=[]

points=[(923.9,562.0244,0),(923.9,570.8333,0),(914.1,568.6853,0),(923.9,554.1,0),(928.8,568.6853,0),(919,579.2,0)]
areas=[787.2464,368.97,73984.02,42012.99,73984.02,44627.33]
print 'points Inter '.join(map(str,points));
print 'Areas Inter '.join(map(str,areas));

for i in range(0,len(areas)-1):
    if p.getDistance(points[i],pedges[i])< 0.0001: #Distance between a point and an edge
        areas_onEdge.append(areas[i])
    else:
        areas_inside.append(areas[i])

result=sum(areas_inside)/areaShell

EDITED

Based on Matheus answer:

for i in range(len(areas)):
    is_on_edge = False

    for j in range(len(pedges)):
        if p.getDistance(points[i], pedges[i]) < 0.0001:
            areas_onEdge.append(areas[i])
            is_on_edge = True

    if not is_on_edge:
        areas_inside.append(areas[i])

Would it be possible to zip three lists like this to get the ratio 'ratt'?

for point, area,areaR in zip(pointsInter_proj, areasInter,areasRev):
    edges_distances = [p.getDistance(point, pedge) for pedge in pedges]
    if min(edges_distances) >=0:
        ratt.append(area/areaR);
user3641829
  • 261
  • 2
  • 11

1 Answers1

0

Probably, your problem lies in the for-loop.

First, you aren't iterating through all your areas since your range goes only up to len(areas)-1. In Python, range's end delimiter is not included in the generated list, e.g., range(0, 5) generates the list [0, 1, 2, 3, 4].

Also, you're comparing each point to only one edge and, as far as I understood, you want to compare a point against all edges.

This loop corrects both points that I presented here:

for i in range(len(areas)):
    is_on_edge = False

    for j in range(len(pedges)):
        if p.getDistance(points[i], pedges[j]) < 0.0001:
            areas_onEdge.append(areas[i])
            is_on_edge = True
            break

    if not is_on_edge:
        areas_inside.append(areas[i])

This is another way of doing the same thing (and more Pythonic in my opinion):

for point, area in zip(points, areas):
    edges_distances = [p.getDistance(point, pedge) for pedge in pedges]

    if min(edges_distances) < 0.0001:
        areas_onEdge.append(area)
    else:
        areas_inside.append(area)
Matheus Portela
  • 2,420
  • 1
  • 21
  • 32
  • 1
    For your first solution, you should break out of your `j` loop if found. There is no need to continue iterating after finding. Also, `pedges[j]` – Lafexlos Feb 26 '16 at 13:12
  • Would it be possible to do something similar zipping 3 lists? See my editing – user3641829 Feb 26 '16 at 16:24
  • Sure, you can zip 3 lists together. Note that zip will cap all lists to the size of the smallest list. E.g. if two lists `a, b` have 10 elements each and a third one `c` contains only 5 elements, the zipped list `zip(a, b, c)` will contain 5 elements. See more here: http://stackoverflow.com/questions/13704860/zip-lists-in-python – Matheus Portela Feb 26 '16 at 19:35