Last active
December 9, 2015 20:58
-
-
Save jeanpat/4327065 to your computer and use it in GitHub Desktop.
Given four points in a plane, defined by their coordinates, there are 4!=24 permutations of those points.
The scripts computes the area of the 24 different quadrilaterals with the shoelace formula and plot them.
The maximal area found, seems to correspond to non self crossing direct quadrilaterals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Dec 18 08:33:15 2012 | |
Modif 3 january 2013 to use matplotlib.patches.Polygon | |
@author: Jean-Patrick Pommier | |
Test the shoelace formula to compute the area of a quadrilateral: | |
. Chose four points | |
. test if self intersection occurs: | |
http://www.bryceboe.com/2006/10/23/line-segment-intersection-algorithm/ | |
. compute area: | |
http://en.wikipedia.org/wiki/Shoelace_formula | |
""" | |
import itertools as it | |
from matplotlib import pyplot as plt | |
from matplotlib.patches import Polygon | |
def quadAreaShoelace(A,B,C,D): | |
x1 = A[0] | |
y1 = A[1] | |
x2 = B[0] | |
y2 = B[1] | |
x3 = C[0] | |
y3 = C[1] | |
x4 = D[0] | |
y4 = D[1] | |
#sign +/- if direct/indirect quadrilateral | |
return 0.5*(x1*y2+x2*y3+x3*y4+x4*y1-x2*y1-x3*y2-x4*y3-x1*y4) | |
def ccw(A,B,C): | |
return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0]) | |
def intersect(A,B,C,D): | |
# Return true if line segments AB and CD intersect | |
return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D) | |
A = (1.1,0.3) | |
B = (1.7,1.4) | |
C = (1,2) | |
D = (0,1) | |
n = 1 | |
for quad in it.permutations((A,B,C,D)): | |
area = quadAreaShoelace(quad[0],quad[1],quad[2],quad[3]) | |
print quad, intersect(quad[0],quad[1],quad[2],quad[3]), area | |
plt.subplot(4,6,n,frameon = True, xticks = [], yticks = [])#xticks = [], yticks = [] | |
plt.xlim((0,2)) | |
plt.ylim((0,2)) | |
startpoint=quad[0] | |
plt.scatter(startpoint[0],startpoint[1],c='m',s=100) | |
for i in range(len(quad)): | |
point1 = quad[i] | |
point2 = quad[(i+1)%4] | |
x = point1[0] | |
y = point1[1] | |
nextpoint = quad[(i+1)%4] | |
dx = nextpoint[0]-x | |
dy = nextpoint[1]-y | |
if area<0: | |
Color='blue' | |
if area == 0: | |
Color='black' | |
if area > 0: | |
Color = 'red' | |
plt.arrow(x,y,dx,dy, color=Color,shape='full', lw=1, length_includes_head=True, head_width=.2) | |
p = Polygon( quad, alpha=0.2, color='g' ) | |
plt.gca().add_artist(p) | |
n = n+1 | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment