def editBoxesToSail(self): try: return Sail( peak=Point(int(self.peak_x.get()), int(self.peak_y.get())), throat=Point(int(self.throat_x.get()), int(self.throat_y.get())), tack=Point(int(self.tack_x.get()), int(self.tack_y.get())), clew=Point(int(self.clew_x.get()), int(self.clew_y.get()))) except ValueError as e: messagebox.showerror("Error on coordinates.", "All edit boxes must have a positive integer: \n" + str(e)) raise
def intersection(line_a, line_b): method="intersection" # y = mx + b m1=line_a.slope m2=line_b.slope #print("slope1="+str(m1)) #print("slope2="+str(m2)) b1=yIntercept(m1, line_a.point_a); b2=yIntercept(m2, line_b.point_b); #print("yIntercept1="+str(b1)) #print("yIntercept2="+str(b2)) #print(type(m1)) #print(type(b1)) # To find the midpoint # y = mx + b print(method + ": b1="+str(b1)+", b2="+str(b2) + ", m1=" + str(m1) + ", m2=" + str(m2)) # if Slopes are the same, # lines never intersect... # or at least have no single point of intersect if (m1 == m2): return None x = int((b2 -b1)/(m1-m2)) y = int(m1*(float(b2-b1)/float(m1-m2)) + float(b1)) return Point(x,y)
def newPointOnLine(slope, newX, existingPoint): if slope is None or newX is None or existingPoint is None: raise ValueError("newPointOnLine(slope, newX, ExistingPoint): <-- none of args may be None!") #y=slope*x+b <-- we need to determine what B is ==> b=y-slope*x # that's why we needed an existing point to start with. b= float(existingPoint.getY())-float(slope)*float(existingPoint.getX()) #y=mx+b newY = slope*newX+int(b) return Point(newX, int(newY))
def getMidpoint(self): self.validate() return Point(int((self.point_a.x+self.point_b.x)/2), int((self.point_a.y+self.point_b.y)/2))
if not isinstance(point_b, Point): raise ValueError("point_b is not a point: " + str(point_b)) self.point_a = point_a self.point_b = point_b self.validate() ################################################################ def __str__(self): return "LineSegement=[" + str(self.point_a) + ", " + str(self.point_b) + "]" ################################################################ def validate(self): if self.point_a is None or self.point_b is None: raise ValueError("LineSegement: both points must be non-null. " + str(self)) ################################################################ def getMidpoint(self): self.validate() return Point(int((self.point_a.x+self.point_b.x)/2), int((self.point_a.y+self.point_b.y)/2)) ################################################################ # M A I N ################################################################ if __name__ == "__main__": print(str(LineSegment(Point(0,2), Point(0,4)).getMidpoint().isEqual(Point(0,3)))) #assert( False == LineSegment(Point(0,2), Point(0,4)).getMidpoint().isEqual(Point(0,3))), print("Yahoo")
lineSegments.append(LineSegment(midpoint_a, self.point_a)) return lineSegments ################################################################ def getCentroidPoint(self): self.validate() # the centroid is the intersection of the centroid line segments. lineSegments = self.getCentroidLineSegments() l1 = Line(lineSegments[0].point_a, lineSegments[0].point_b) l2 = Line(lineSegments[1].point_a, lineSegments[1].point_b) coe = intersection(l1, l2) return coe ################################################################ # M A I N ################################################################ if __name__ == "__main__": t = Triangle(Point(0,0), Point(10,0), Point(5,5)) print("Area of triangle " + str(t) + " is " + str(t.area())) print("Line segmetns of " + str(t) + " is ==> ") for segment in t.getCentroidLineSegments(): print("\t" + str(segment))
# or at least have no single point of intersect if (m1 == m2): return None x = int((b2 -b1)/(m1-m2)) y = int(m1*(float(b2-b1)/float(m1-m2)) + float(b1)) return Point(x,y) ######################################################### # M A I N ######################################################### if __name__ == "__main__": # just some tests... l = Line(Point(1,1),Point(4,4)) print(l) #print (Line (None, None, None)) print(str(getSlope(Point(50,10), Point(24,115)))) try: print( str (Line(Point(1,1), None, 1.0))) raise ValueError("Expected a value error here!!!") except ValueError: print('Expected error')
def getSailPointFromJson(pointName, jsonData): for sailPoint in jsonData["sail_points"]: if (pointName == sailPoint["location"]): return Point(sailPoint["coordinates"][0], sailPoint["coordinates"][1]) return None
clew = getSailPointFromJson("clew", jsonData) throat = getSailPointFromJson("throat", jsonData) head = getSailPointFromJson("head", jsonData) sail = Sail(peak=peak, tack=tack, clew=clew, throat=throat, head=head, sailName=sailName) return sail ########################################## # # Just for Testing # ########################################## if __name__ == "__main__": x = Sail( peak=Point(310, 400), throat=Point(60, 240), tack=Point(10, 20), clew=Point(350, 15), ) print(str(x.getNumSides())) print(str(x.getCoordinatesAsSingleVector())) x = Sail(Point(6, 7), Point(7, 8), Point(8, 9)) print(str(x.getNumSides())) #x = Sail(1, 2, None, None, 5)