Commit 8a66719c authored by ronan's avatar ronan
Browse files

FindMin fonctionne

parents
class BeachLine:
def __init__(self):
self.dico = {}
def insert(self, p0):
pass
\ No newline at end of file
from Vector3D import Vector3D
class Edge:
def __init__(self, vec, p0=None, p1=None):
self.vec = vec
self.p0 = p0
self.p1 = p1
from Site import Site
from VSite import VSite
from BeachLine import BeachLine
from Vector3D import Vector3D
import math
class Fortune:
def __init__(self, points):
self.points = points
self.sites = []
self.events = []
self.beachLine = BeachLine()
# Sort Y
for i in range(len(self.points)):
for j in range(i+1, len(self.points)):
if self.points[i].getY() < self.points[j].getY():
self.points[i], self.points[j] = self.points[j], self.points[i]
# Convert points into sites and create events
for i in range(len(self.points)):
site = Site(self.points[i])
self.sites.append(site)
self.events.append(site)
#self.create()
def create(self):
while len(self.events) != 0:
if type(self.events[len(self.events)-1]) == VSite:
self.handleVertex()
else:
self.handleSite()
def findMinCircle(self, p1, p2, p3):
x1 = p1.point.getX(); x1_2 = x1*x1; x2 = p2.point.getX(); x2_2 = x2*x2; x3 = p3.point.getX(); x3_2 = x3*x3;
y1 = p1.point.getY(); y1_2 = y1*y1; y2 = p2.point.getY(); y2_2 = y2*y2; y3 = p3.point.getY(); y3_2 = y3*y3;
#First part
h = x2_2 + y2_2 - (y1_2 + x1_2)
b = 2*(x2 - x1)
a = (y1 - y2)
#Second Part
f = x3_2 + y3_2 - (y1_2 + x1_2)
d = 2*(y3 - y1)
e = (x1 - x3)
#Determine Center
x = ((2*a*f/d) + h)/(b*(1-(4*a*e/(d*b))))
y = (2*x*e + f)/d
center = Vector3D(x, y)
magn = math.sqrt((x - x1)*(x - x1) + (y - y1)*(y - y1))
minY = Vector3D(x, y + magn)
vSite = VSite(minY, center)
return vSite
def insertBeachLine(self, p0):
pass
def handleSite(self):
site = self.events.pop(len(self.events)-1) #SweapLine
[p1, p, p3] = self.beachLine.insert(site)
if p1 is not None and p is not None and p3 is not None:
if p1 is not None:
min = self.findMinCircle(p1, p, site)
if min.point.y > site.point.y:
self.addEvent(min)
if p3 is not None:
min = self.findMinCircle(p1, p, site)
if min.point.y > site.point.y:
self.addEvent(min)
def handleVertex(self):
vSite = self.events.pop(len(self.events)-1)
def addEvent(self, site):
pass
\ No newline at end of file
from Vector3D import Vector3D
class Site:
def __init__(self, point):
self.point = point
self.sites = {}
self.edges = {}
from Site import Site
class VSite (Site):
def __init__(self, point, center):
Site.__init__(self, point)
self.center = center
import numpy
import math
"""
Calcul et Gestion Vector3D
Le vecteur 3D comporte 4 valeurs.
Les 3 premières concernent les coordonnées
La dernière: 0 si c'est un vecteur
1 si c'est un point
-> Simplification calcul avec les matrices 4x4
"""
class Vector3D:
def __init__(self, x=0.0, y=0.0, z=0.0, w=1):
if type(x) == Vector3D:
self.coor = (y - x).getCoor()
else:
self.coor = numpy.array([float(x), float(y), float(z), float(w)])
#====================================================
# Getter
#====================================================
def copy(self):
return Vector3D(self.getX(), self.getY(), self.getZ(), self.getW())
def getTransformation(self, matrix):
return matrix.dot(self.coor)
def project(self, projectMatrix):
return projectMatrix.transform(self)
def getCoor(self):
return self.coor
def getX(self):
return self.coor[0]
def getY(self):
return self.coor[1]
def getZ(self):
return self.coor[2]
def getW(self):
return self.coor[3]
def isPoint(self):
return bool(self.coor[3])
def getMagnitude(self):
return math.sqrt(self.getSqMagnitude())
def getSqMagnitude(self):
return float(self.scalar(self))
#====================================================
# Setter
#====================================================
def getNormalize(self):
vec = self.copy()
vec.normalize()
return vec
def setCoor(self, coor):
for i in range(4):
self.coor[i] = coor[i]
def setX(self, x):
self.coor[0] = float(x)
def setY(self, y):
self.coor[1] = float(y)
def setZ(self, z):
self.coor[2] = float(z)
def setW(self, w):
self.coor[3] = float(w)
def set(self, x, y=0, z=0, w=1):
if type(x) == Vector3D:
self.setCoor(x.getCoor())
else:
self.coor[0] = float(x)
self.coor[1] = float(y)
self.coor[2] = float(z)
self.coor[3] = float(w)
def setIsPoint(self, isPoint):
self.coor[3] = (1 if isPoint else 0)
@staticmethod
def create(coor): # coor is an array
return Vector3D(coor[0], coor[1], coor[2], coor[3])
@staticmethod
def createFromMatrix(coor):
return Vector3D(coor[0,0], coor[0,1], coor[0,2], coor[0,3])
#====================================================
# Operators
#====================================================
def getRotatedZ(self, theta):
return Vector3D(self.coor[0] * math.cos(theta) - self.coor[1] * math.sin(theta),
self.coor[1] * math.cos(theta) + self.coor[0] * math.sin(theta),
self.coor[2], self.coor[3])
def neg(self):
return Vector3D.create(-self.coor)
def __neg__(self):
return self.neg()
def selfAdd(self, vec3D):
# marche?
self.coor += vec3D.getCoor()
def __iadd__(self, vec3D):
self.selfAdd(vec3D)
return self
def add(self, vec3D):
nump = self.coor + vec3D.getCoor()
return Vector3D.create(nump)
def __add__(self, vec3D):
return self.add(vec3D)
def sub(self, vec3D):
nump = self.coor - vec3D.getCoor()
return Vector3D.create(nump)
def __sub__(self, vec3D):
return self.sub(vec3D)
def selfSub(self, vec3D):
self.coor -= vec3D.getCoor()
def __isub__(self, vec3D):
self.selfSub(vec3D)
return self
def cross(self, vec3D):
return Vector3D(self.getY()*vec3D.getZ() - self.getZ()*vec3D.getY(),
self.getZ()*vec3D.getX() - self.getX()*vec3D.getZ(),
self.getX()*vec3D.getY() - self.getY()*vec3D.getX(),
self.getZ())
def __cross__(self, vec3D):
return self.cross(vec3D)
def selfCross(self, vec3D):
self.set(self.getY()*vec3D.getZ() - self.getZ()*vec3D.getY(),
self.getZ()*vec3D.getX() - self.getX()*vec3D.getZ(),
self.getX()*vec3D.getY() - self.getY()*vec3D.getX())
def __icross__(self, vec3D):
self.selfCross(vec3D)
return self
def __mul__(self, f):
if type(f) == Vector3D:
return self.scalar(f)
else:
return self.multiply(f)
def __imul__(self, f):
assert(type(f) != Vector3D)
self.selfMultiply(f)
return self
def __str__(self):
return str(self.coor)
def scalar(self, vec3D):
return self.getX()*vec3D.getX() + self.getY()*vec3D.getY() + self.getZ()*vec3D.getZ()
def multiply(self, s1):
s = float(s1)
return Vector3D(self.coor[0]*s, self.coor[1]*s, self.coor[2]*s, self.coor[3])
def multiplyVec(self, vec):
return Vector3D(self.coor[0]*vec.getX(), self.coor[1]*vec.getY(), self.coor[2]*vec.getZ(), self.coor[3])
def selfMultiply(self, s1):
s = float(s1)
for i in range(3):
self.coor[i] *= s
def normalize(self):
magn = self.getMagnitude()
if magn != 0:
self.selfMultiply(1.0/magn)
return magn
def disp(self):
print(self)
@staticmethod
def createVec(v1, v2):
return v2.sub(v1)
def draw(self, screen, color = "black"):
screen.create_oval(self.getX() - 5, self.getY() - 5, self.getX() + 5, self.getY() + 5, fill=color)
#screen.create_line(self.getX(), self.getY(), self.getX(), self.getY() + 1)
\ No newline at end of file
from tkinter import *
from Fortune import Fortune
from Vector3D import Vector3D
import math
"""
def clavier(event):
global coords
touche = event.keysym
canvas.delete(ALL)
#Analisis
if len(events) != 0:
if events[0][1].getY() < lY.getY():
if events[0][0] == 0: # add site
a = events.pop(0)
addSite(a, vec, lY.getY(), )
elif events[0][0] == 1: # circle event
pass
lY.setY(lY.getY() + 5)
#Check parabola
checkParabola(vec, beachLine, lY)
for i in range(len(beachLine)):
parabola[beachLine[i]][1] = lY.getY()
for i in range(len(parabola)):
draw_Pol(canvas, parabola[i][0], parabola[i][1])
canvas.create_line(0, lY.getY(), lY.getX(), lY.getY())
for i in range(len(vec)):
vec[i].draw(canvas)
def addSite(a, vec, ly, beachLine, parabola, actual):
parabola.append([a[1], ly + 0.1])
beachLine.append(len(parabola) - 1)
actual[0] += 1
if actual[0] < len(vec):
events.append([0, vec[actual[0]]])
def checkParabola(vec, beachLine, lY):
x = len(beachLine) - 1
while x > -1:
a = vec[beachLine[x]]
j = 0
while j < x:
if x != j:
b = vec[beachLine[j]]
veca = b-a
magn = veca.getMagnitude()
#print(magn)
print(lY.getY() - b.getY())
if lY.getY() - b.getY() > magn:
beachLine.pop(j)
j -= 1
x -= 1
break
j += 1
x -= 1
def draw_Pol(screen, p, y):
fac = 10
a = p.getY()*p.getY() - y*y
b = (2*(y - p.getY()))
for x in range(500):
x1 = x + p.getX() - 250
h = (p.getX() - x1)*(p.getX() - x1) + a
y1 = -h/b
screen.create_oval(x1 - 1, y1 - 1, x1 + 1, y1 + 1, fill='black')
if __name__ == "__main__":
fenetre = Tk()
canvas = Canvas(fenetre, width=800, height=800, background='white')
canvas.focus_set()
canvas.bind("<Key>", clavier)
fac = Vector3D(60, 60)
vec = []
vec.append(Vector3D(3, 2))
vec.append(Vector3D(4, 3))
vec.append(Vector3D(5, 5))
vec.append(Vector3D(6, 6))
vec.append(Vector3D(2, 6.2))
vec.append(Vector3D(3.5, 7))
for i in range(len(vec)):
vec[i].setX(vec[i].getX() * fac.getX())
vec[i].setY(vec[i].getY() * fac.getY())
vec[i].draw(canvas)
#Start
lY = Vector3D(500, vec[0].getY())
actual = [0]
edges = []
vertex = []
parabola = []
beachLine = []
events = []
events.append([0, vec[0]])
canvas.pack()
fenetre.mainloop()
"""
if __name__ == "__main__":
fenetre = Tk()
canvas = Canvas(fenetre, width=800, height=800, background='white')
vec = []
vec.append(Vector3D(3, 2))
vec.append(Vector3D(4, 3))
vec.append(Vector3D(6, 6))
vec.append(Vector3D(3.5, 7))
vec.append(Vector3D(2, 6.2))
vec.append(Vector3D(5, 5))
fac = Vector3D(60, 60)
for i in range(len(vec)):
vec[i].setX(vec[i].getX() * fac.getX())
vec[i].setY(vec[i].getY() * fac.getY())
canvas.focus_set()
#canvas.bind("<Key>", clavier)
f = Fortune(vec)
canvas.pack()
fenetre.mainloop()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment