def getPointsWithinCorners(self, corners): """Return the segment made of the poins of the line which are in the area delimited by the corners.""" xmin, ymin, xmax, ymax = corners p1 = Point(xmin, ymin) p2 = Point(xmax, ymin) p3 = Point(xmax, ymax) p4 = Point(xmin, ymax) v1 = Vector(p1, p2) v2 = Vector(p2, p3) v3 = Vector(p3, p4) v4 = Vector(p4, p1) l1 = Line.createFromPointAndVector(p1, v1) l2 = Line.createFromPointAndVector(p2, v2) l3 = Line.createFromPointAndVector(p3, v3) l4 = Line.createFromPointAndVector(p4, v4) lines = [l1, l3] points = [] for line in lines: cross = self.crossLine(line) if cross: points.append(cross) if not points: lines = [l2, l4] for line in lines: cross = self.crossLine(line) if cross: points.append(cross) print("points: ", points) return points
def getSegmentWithinYRange(self, ymin, ymax): """Return the segment made of the points of the line which y component is between ymin and ymax.""" xymin = self.devaluate(ymin) xymax = self.devaluate(ymax) p1 = Point(xymin, ymin) p2 = Point(xymax, ymax) return Segment(p1, p2, width=self.width, color=self.color)
def getSegmentWithinXRange(self, xmin, xmax): """Return the segment made of the points of the line which x component is between xmin and xmax.""" yxmin = self.evaluate(xmin) yxmax = self.evaluate(xmax) p1 = Point(xmin, yxmin) p2 = Point(xmax, yxmax) return Segment(p1, p2)
def __init__(self): points = [Point(0, 2), Point(1, -1), Point(0, 0), Point(-1, -1)] self.life = None self.max_life = 100 self.damage = 10 self.motion = Motion([0, 0]) self.displacement = [Motion([0, 0]), Motion([0, 0])] Form.__init__(self, points)
def __contains__(self, point): """Return the boolean: (the point is in the form).""" x, y = point[0], point[1] p1 = Point(x, y) p2 = Point(0, 0) line = Line(p1, p2) line.show(window) for segment in self.sides(): if segment | line: return True
def oldgetSegmentWithinCorners(self, corners): """Return the segment made of the poins of the line which are in the area delimited by the corners.""" xmin, ymin, xmax, ymax = corners yxmin = self.evaluate(xmin) yxmax = self.evaluate(xmax) xymin = self.devaluate(ymin) xymax = self.devaluate(ymax) nxmin = max(xmin, xymin) nymin = max(ymin, yxmin) nxmax = min(xmax, xymax) nymax = min(ymax, yxmax) p1 = Point(nxmin, nymin) p2 = Point(nxmax, nymax) return Segment(p1, p2, width=self.width, color=self.color)
def center(self, color=None, radius=None): """Return the point of the center.""" if not color: color = self.point_color if not radius: radius = self.point_radius mx = mean([p.x for p in self.points]) my = mean([p.y for p in self.points]) return Point(mx, my, color=color, radius=radius)
def getFormToScreen(self, plane_form, window): """Create a new form according screen coordonnates using a form and the window.""" points = [ Point(self.getToScreen(point, window)) for point in plane_form ] screen_form = deepcopy(plane_form) screen_form.points = points return screen_form
def random(points_number=None, min=-1, max=1, **kwargs): """Create a random form using the point_number, the minimum and maximum position for x and y components and optional arguments.""" if not points_number: points_number = random.randint(1, 10) points = [Point.random(min, max) for i in range(points_number)] form = Form(points, **kwargs) print(form.point_radius) form.makeSparse() return form
def oldgetSegmentWithinCorners(self, corners): """Return the segment made of the poins of the line which are in the area delimited by the corners.""" xmin, ymin, xmax, ymax = corners p1 = Point(xmin, ymin) p2 = Point(xmax, ymin) p3 = Point(xmax, ymax) p4 = Point(xmin, ymax) s1 = Segment(p1, p2) s2 = Segment(p2, p3) s3 = Segment(p3, p4) s4 = Segment(p4, p1) segments = [s1, s2, s3, s4] intersections = [] print(segments) for segment in segments: cross = self.crossSegment(segment) if cross: intersections.append(cross) print("intersections: ", intersections) return Segment(intersections)
def crossLine(self, other): """Return the point of intersection between two lines with vectors calculation.""" a, b = self.point c, d = other.point m, n = self.vector() o, p = other.vector() if n * o - m * p == 0: return None #The lines are parallels x = (a * n * o - b * m * o - c * m * p + d * m * o) / (n * o - m * p) y = (x - a) * n / m + b rx = round(x, 15) ry = round(y, 15) return Point(rx, ry)
def oldcrossLine(self, other): """Return the point of intersection between the two lines.""" if self.parallel(other): return None #Extract the slopes and ordinates sb = self.ordinate() ob = other.ordinate() sa = self.slope() oa = other.slope() #Find a possible intersection point using line intersection x = (sb - ob) / (oa - sa) y = sa * x + sb #Return the intersection point return Point(x, y, color=self.color)
def crossLine(self,other): """Determine if the segment is crossing with a line.""" if self.parallel(other): return None #If the segments are parallels then there is not point #Extract the slopes and ordinates sb=self.ordinate() ob=other.ordinate() sa=self.slope() oa=other.slope() #Find a possible intersection point using line intersection x=(sb-ob)/(oa-sa) y=sa*x+sb #Determine if the point of intersection belongs to both the segment and the line xmin,ymin,xmax,ymax=self.getCorners() #If it is the case return the point if xmin<=x<=xmax and ymin<=y<=ymax: return Point(x,y,color=self.color)
def getFormToScreen(self, plane_form, window): """Create a new form according screen coordonnates using a form and the window.""" #This function deals with an issue which is that the forms that are #defined uses screen coordonnates instead of the plane's one. #This problem has been fixed in more recent versions by changing the #nature of the windows given to the show functions of the forms by #defining a new type of window which is called 'Surface' for now... points = [ Point(self.getToScreen(point, window), radius=1) for point in plane_form ] for point in points: point.truncate() screen_form = deepcopy(plane_form) screen_form.points = points return screen_form
def addPointToForm(self, screen_position, window, selection=None): """Add a point to the selected form.""" if not selection: selection = self.selection position = self.getFromScreen(screen_position, window) point = Point(position) self.forms[selection] += point
def startForm(self, screen_position, window): """Start a new form to create using screen_position and window.""" position = self.getFromScreen(screen_position, window) point = Point(position) self.selection = len(self.forms) self.forms.append(Form([point]))
def center(self,color=None): """Return the point of the center.""" if not color: color=self.point_color mx=mean([p.x for p in self.points]) my=mean([p.y for p in self.points]) return Point(mx,my,color=color)
from mypoint import Point from myform import Form from myvector import Vector from mywindow import Window from random import randint window=Window() wsx,wsy=window.size X=wsx//4 Y=wsy//4 posAlea=lambda :[randint(X,3*X),randint(Y,3*Y)] f1=Form([Point(*posAlea()) for i in range(5)]) f1=f1.getSparse() f2=Form([Point(*posAlea()) for i in range(5)]) f2=f2.getSparse() #f1.points[0].color=(255,0,0) #f1.points[1].color=(255,0,0) while window.open: window.check() window.clear() f1.rotate(0.01,f1[0]) #f1.rotate(-0.01) f2.rotate(0.02,f1[1]) f2.rotate(-0.03) f1.show(window)
def showMotion(self,window): """Show the motion of a material point on the window.""" position,velocity,acceleration=self.motion #Extract the vectors out of the motion. point=Point(tuple(position)) velocity.show(point,window) acceleration.show(point,window)
def show(self,window): """Show the material point on the window.""" position=self.getPosition() x,y=position point=Point(x,y) point.show(window)
def norm(self): """Return the angle of a vector with the [1,0] direction in cartesian coordonnates.""" return Vector.polar([self.x, self.y])[0] def __xor__(self, other): """Return the angle between two vectors.""" return self.angle() - other.angle() def __invert__(self): """Return the unit vector.""" n, a = self.polar() if __name__ == "__main__": window = Window("Vector") p1 = Point(50, 100) p2 = Point(500, 400) p3 = Point(300, 200) v1 = Vector(p1, p2) v2 = 0.8 * v1 p4 = v2(p1) v2.color = (255, 0, 0) p4.color = (0, 255, 0) v1.show(p1, window) v2 %= 0.3 v2.show(p1, window) p2.show(window) p4.show(window) window()
def center(self): """Return the point of the center of the segment.""" x=(self.p1.x+self.p2.x)/2 y=(self.p1.y+self.p2.y)/2 return Point(x,y,color=self.color)
def random(min=-1,max=1,width=1,color=mycolors.WHITE): """Create a random segment.""" p1=Point.random(min,max) p2=Point.random(min,max) return Segment(p1,p2,width,color)
def rotate(self, angle, point=Point(0, 0)): """Rotate the line.""" #Incomplete self.angle += angle self.correctAngle()
def correctPoint(self): """Correct the point to the definition of the neighbour point.""" origin = Point([0, 0]) point = self.projectPoint(origin) self.point = point
if not width: width = self.width corners = surface.getCorners() print("corners: ", corners) points = self.getPointsWithinCorners(corners) print(points) p1, p2 = points #print("points: ",segment.p1,segment.p2) surface.draw.line(surface.screen, color, [p1.x, p1.y], [p2.x, p2.y], width=width) if __name__ == "__main__": from mysurface import Surface surface = Surface(fullscreen=True) p1 = Point(1, 0, radius=0.05, color=mycolors.YELLOW) p2 = Point(1, 1, radius=0.05, color=mycolors.YELLOW) origin = Point(0, 0) print("Creating Line") l1 = Line(origin, math.pi / 4, correct=False) l2 = Line(p1, math.pi / 2, correct=False) print("l1.vector: ", l1.vector()) print("l2.vector: ", l2.vector()) intersection = l1.crossLine(l2) print("intersection: ", intersection) print("point: ", l2.point) #surface.draw.window.pause() print("scalar product:", p2 in l1) #surface()
def random(min=-1, max=1, width=1, color=mycolors.WHITE): """Return a random line.""" point = Point.random(min, max) angle = random.uniform(min, max) return Line(point, angle, width, color)
def __init__(self): points = [Point(0, 10), Point(10, -10), Point(-10, -10)] self.life = None self.max_life = 100 self.damage = 10 Form.__init__(self, points)
from mysurface import Surface2 #from mymaths import sign2,mean from math import sqrt, atan, pi, cos, sin from cmath import polar from mypoint import Point from myform import Form from myvector import Vector import random import time if __name__ == "__main__": window = Surface2() p1 = Point(15, 62) p2 = Point(250, 400) p3 = Point(800, 500) p4 = Point(400, 400, color=(0, 255, 0)) points = [p1, p3, p2, p4] f = Form([ Point(random.randint(-10, 10), random.randint(-10, 10)) for i in range(10) ]) #f.show(window) f2 = f.getSparse() p1.show(window) p2.show(window) v = Vector(p2[0] - p1[0], p2[1] - p1[1], color=(255, 0, 0)) center = f2.center() while window.open:
from myform import Form from mypoint import Point class Set: def __init__(self, forms): self.forms = forms def move(self): pass def control(self): pass if __name__ == "__main__": forms = [ Form([ Point(random.randint(-10, 10), random.randint(-10, 10)) for i in range(10) ]) for i in range(3) ] set = Set(forms) surface = Surface() while surface.open: surface.check() set.control() surface.clear() set.show() surface.flip()