1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
r = 10 # Dimension du cercle
def est_au_dessus(XY,Droite):
x, y = tuple(XY)
xA, yA, xB, yB = tuple(Droite)
# Le point ne peut être au dessus d'une droite verticale
if xB == xA:
return False
a= (yB-yA)/(xB-xA)
b= yA-a*xA
valeur_a_retourner = y<a*x+b
return valeur_a_retourner
def est_en_dessous(XY,Droite):
x, y = tuple(XY)
xC, yC, xD, yD = tuple(Droite)
if xC == xD:
return False
# Le point ne peut être au dessous d'une droite verticale
a= (yD-yC)/(xD-xC)
b= yC-a*xC
valeur_a_retourner = y>a*x+b
return valeur_a_retourner
def est_a_gauche(XY,Droite):
x, y = tuple(XY)
xA, yA, xC, yC = tuple(Droite)
if yA == yC:
return False
# Le point ne peut être à gauche d'une droite horizontale
a= (xC-xA)/(yC-yA)
b= xA-a*yA
valeur_a_retourner=x<a*y+b
return valeur_a_retourner
def est_a_droite(XY,Droite):
x, y = tuple(XY)
xB, yB, xD, yD = tuple(Droite)
if yB == yD:
return False
#Le point ne peut être à droite d'une droite horizontale
a= (xD-xB)/(yD-yB)
b= xB-a*yB
valeur_a_retourner=x>a*y+b
return valeur_a_retourner
def est_dans_polygone(XY,r, Droite1, Droite2):
Droite3 = [Droite1[0], Droite1[1], Droite2[0], Droite2[1]]
Droite4 = [Droite1[2], Droite1[3], Droite2[2], Droite2[3]]
est_dans_poly1 = est_en_dessous([XY[0], XY[1] - r], Droite2) and \
est_au_dessus([XY[0],XY[1] + r], Droite1) and \
est_a_droite([XY[0] - r,XY[1]], Droite3) and \
est_a_gauche([XY[0] + r,XY[1]], Droite4)
est_dans_poly2 = est_en_dessous([XY[0], XY[1] - r], Droite1) and \
est_au_dessus([XY[0],XY[1] + r], Droite2) and \
est_a_droite([XY[0] - r,XY[1]], Droite4) and \
est_a_gauche([XY[0] + r,XY[1]], Droite3)
est_dans_poly3 = est_en_dessous([XY[0], XY[1] - r], Droite3) and \
est_au_dessus([XY[0],XY[1] + r], Droite4) and \
est_a_droite([XY[0] - r,XY[1]], Droite2) and \
est_a_gauche([XY[0] + r,XY[1]], Droite1)
est_dans_poly4 = est_en_dessous([XY[0], XY[1] - r], Droite3) and \
est_au_dessus([XY[0],XY[1] + r], Droite4) and \
est_a_droite([XY[0] - r,XY[1]], Droite1) and \
est_a_gauche([XY[0] + r,XY[1]], Droite2)
est_dans_poly5 = est_en_dessous([XY[0], XY[1] - r], Droite4) and \
est_au_dessus([XY[0],XY[1] + r], Droite3) and \
est_a_droite([XY[0] - r,XY[1]], Droite2) and \
est_a_gauche([XY[0] + r,XY[1]], Droite1)
est_dans_poly6 = est_en_dessous([XY[0], XY[1] - r], Droite4) and \
est_au_dessus([XY[0],XY[1] + r], Droite3) and \
est_a_droite([XY[0] - r,XY[1]], Droite1) and \
est_a_gauche([XY[0] + r,XY[1]], Droite2)
return est_dans_poly1 or est_dans_poly2 or est_dans_poly3 or est_dans_poly4 or est_dans_poly5 or est_dans_poly6
# Code:
#######
print(est_dans_polygone([3,0], 0.2, [2,5,3,3], [1,-1,5,-1])) |
Partager