Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: implement a script in a function. Some suggestions
    primarykey
    data
    text
    <p>first of all, i am quite new in Python (an programming area) but i wish to learn and convert a function developed by <a href="https://stackoverflow.com/users/837847/jwpat7">jwpat7</a>. Given a set of points derived from a convex hull</p> <pre><code>hull= [(560023.44957588764,6362057.3904932579), (560023.44957588764,6362060.3904932579), (560024.44957588764,6362063.3904932579), (560026.94957588764,6362068.3904932579), (560028.44957588764,6362069.8904932579), (560034.94957588764,6362071.8904932579), (560036.44957588764,6362071.8904932579), (560037.44957588764,6362070.3904932579), (560037.44957588764,6362064.8904932579), (560036.44957588764,6362063.3904932579), (560034.94957588764,6362061.3904932579), (560026.94957588764,6362057.8904932579), (560025.44957588764,6362057.3904932579), (560023.44957588764,6362057.3904932579)] </code></pre> <p>this script return a print of all possible area following this <a href="https://stackoverflow.com/questions/13542855/python-help-to-implement-an-algorithm-to-find-the-minimum-area-rectangle-for-gi">post problem</a>. The code develop by jwpat7 is: </p> <pre><code>import math def mostfar(j, n, s, c, mx, my): # advance j to extreme point xn, yn = hull[j][0], hull[j][1] rx, ry = xn*c - yn*s, xn*s + yn*c best = mx*rx + my*ry while True: x, y = rx, ry xn, yn = hull[(j+1)%n][0], hull[(j+1)%n][1] rx, ry = xn*c - yn*s, xn*s + yn*c if mx*rx + my*ry &gt;= best: j = (j+1)%n best = mx*rx + my*ry else: return (x, y, j) n = len(hull) iL = iR = iP = 1 # indexes left, right, opposite pi = 4*math.atan(1) for i in range(n-1): dx = hull[i+1][0] - hull[i][0] dy = hull[i+1][1] - hull[i][1] theta = pi-math.atan2(dy, dx) s, c = math.sin(theta), math.cos(theta) yC = hull[i][0]*s + hull[i][1]*c xP, yP, iP = mostfar(iP, n, s, c, 0, 1) if i==0: iR = iP xR, yR, iR = mostfar(iR, n, s, c, 1, 0) xL, yL, iL = mostfar(iL, n, s, c, -1, 0) area = (yP-yC)*(xR-xL) print ' {:2d} {:2d} {:2d} {:2d} {:9.3f}'.format(i, iL, iP, iR, area) </code></pre> <p>the result is:</p> <pre><code>i iL iP iR Area 0 6 8 0 203.000 1 6 8 0 211.875 2 6 8 0 205.800 3 6 10 0 206.250 4 7 12 0 190.362 5 8 0 1 203.000 6 10 0 4 201.385 7 0 1 6 203.000 8 0 3 6 205.827 9 0 3 6 205.640 10 0 4 7 187.451 11 0 4 7 189.750 12 1 6 8 203.000 </code></pre> <p>i wish to create a single function with the return of Length, Width, and Area of the smallest rectangle. Ex:</p> <pre><code>Length, Width, Area = get_minimum_area_rectangle(hull) print Length, Width, Area 18.036, 10.392, 187.451 </code></pre> <p>my questions are:</p> <ol> <li>do i need to create a single function or two function. ex: def mostfar and get_minimum_area_rectangle</li> <li>hull is a list of value. Is it the best format? <ol start="3"> <li>followint the one function approach, i have a problem to integrate mostfar inside </li> </ol></li> </ol> <p>Thanks in advance</p> <p>1) solution: one function following the first solution suggest by Scott Hunter, i have a problem to integrate mostfar() inside get_minimum_area_rectangle(). Any suggestion or help are really appreciate because i can learn. </p> <pre><code>#!/usr/bin/python import math def get_minimum_area_rectangle(hull): # get pi greek pi = 4*math.atan(1) # number of points n = len(hull) # indexes left, right, opposite iL = iR = iP = 1 # work clockwise direction for i in range(n-1): # distance on x axis dx = hull[i+1][0] - hull[i][0] # distance on y axis dy = hull[i+1][1] - hull[i][1] # get orientation angle of the edge theta = pi-math.atan2(dy, dx) s, c = math.sin(theta), math.cos(theta) yC = hull[i][0]*s + hull[i][1]*c </code></pre> <p>from here following the above example of jwpat7 i need to use mostfar(). I have a problem to understand how integrate (sorry for the not right term) mostfar in this point</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload