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
| #objet point
class Point:
#initialisation à partir des coordonnées
def __init__(self, a,b):
self.x=a
self.y=b
#translaté d'un point par un vecteur
def __add__(self,V):
return Point(self.x+V.x,self.y+V.y)
#vecteur comme différence de deux points
def __sub__ (self,other):
return Vecteur(other,self)
#objet vecteur
class Vecteur:
#initialisation à partir de deux points
def __init__(self,A,B):
self.x=B.x-A.x
self.y=B.y-A.y
#somme de deux vecteurs
def __add__(self,other):
X=Point(self.x,self.y)
return X+other
#produit saclaire
def __mul__(self,other):
return self.x*other.x+self.y*other.y
#norme
def __abs__(self):
return self.x*self.x+self.y*self.y
#produit d'un vecteur par un scalaire
def scal(self,m):
x=int(self.x*m)
y=int(self.y*m)
return Point(x,y)-Point(0,0)
#objet rectangle
class Rectangle:
#constructeur avec 3 sommets
def __init__(self,X,Y,Z):
self.A=X
self.B=Y
self.D=Z
self.C= (X+(Y-X))+(Z-X)
#objet segment
class Segment:
#initialisation avec les point extrêmes
def __init__(self,P,Q):
self.M=P
self.N=Q
# le point de paramètre m m dans [0,1]
def Param(self,m):
V=(self.N-self.M).scal(m)
return self.M+V
# coordonnées de V dans la base (V1,V2) les deux vecteurs étant supposés orthogonaux
def coordonnees (V,V1,V2):
return (V*V1)/(V1*V1),(V*V2)/(V2*V2)
#teste si P est dans le rectangle R
def IsIn(P,R):
V1=R.B-R.A
V2=R.D-R.A
V= P-R.A
return 0<=coordonnees(V,V1,V2)[0]<=1 and 0<=coordonnees(V,V1,V2)[1]<=1
#imprime tous les points dans R inter S
def PrintInter(R,S):
V=S.N-S.M
if V.x > V.y : Max=V.x
else : Max=V.y
for i in range(0,Max):
m= i/float(Max)
Mm=S.Param(m)
if IsIn(Mm,R):
print Mm.x,Mm.y
def main():
A=Point(0,100)
B=Point(100,0)
D=Point(200,300)
R=Rectangle(A,B,D)
C=R.C
T1=Point(150,300)
T2=Point(200,0)
S=Segment(T1,T2)
PrintInter(R,S)
if __name__ == '__main__':
main() |
Partager