Convex body region in Python?

Scrolling down: please indicate the reason. Thanks!

I have a set of points A I get the convex hull of CH_A of A

Then I have additional points, a set of points B I add B to A and get a larger set of points. I get the convex hull CH_AB this larger set containing both A and B

I want to quantify how much I have to pay to add B to set A I am thinking of using an additional area to quantify this value.

Say CH_A has Area_A , then CH_AB has Area_AB . Then I want to calculate marginal costs as

 (Area_AB - Area_A) / Area_A 

How can I get the convex hull area in python?

+6
source share
3 answers

A convex body is simply a convex polygon , so you can easily try {this} or {this} to find the area of ​​a two-dimensional polygon.

Something like the following (our version):

 def PolyArea2D(pts): lines = np.hstack([pts,np.roll(pts,-1,axis=0)]) area = 0.5*abs(sum(x1*y2-x2*y1 for x1,y1,x2,y2 in lines)) return area 

in which pts is an array of polygonal vertices, i.e. array (nx2).

Full use:

 import numpy as np def PolyArea2D(pts): lines = np.hstack([pts,np.roll(pts,-1,axis=0)]) area = 0.5*abs(sum(x1*y2-x2*y1 for x1,y1,x2,y2 in lines)) return area pts = [[0,0],[1,0],[1,1],[0,1]] print PolyArea2D(pts) pts = [[0,0],[1,0],[0,1]] print PolyArea2D(pts) pts = [[0,0],[1,0],[0.5,0.5]] print PolyArea2D(pts) >>> 1.0 0.5 0.25 
+8
source

You can simply use the ConvexHull class from scipy.spatial. He will not only give you the enclosure area, but will also calculate the enclosure for you. But if you use it, BEWARE! In 2D, the attribute you want to use is not a scope, it is a volume because the first will actually give you the perimeter of the enclosure.

This is because attributes are named after their values ​​in 3D, where the area will really be the area of ​​the body, and the volume, well, its volume. For two-dimensional cases, the names are the same, but what they actually contain is not quite what he says in tin. Worse, the documentation does not warn you about this.

(You can easily verify this with trivial examples, such as an isosceles right triangle of length 1 on his legs: the perimeter should be 2 + sqrt (2), or about 3.414213562, and the area should be 0.5.)

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html

+3
source

You can use the polygon library to do the math for you: https://pypi.python.org/pypi/Polygon/2.0.4

0
source

Source: https://habr.com/ru/post/957724/


All Articles