def fitCircle3point(self): if len(self.pointList)<3: print "Error: pointListSize<3" firstPoint = self.pointList[0] lastPoint = self.pointList[-1] l1 = line.Line() l1.Line(firstPoint, lastPoint) perpendiculaLine = l1.returnPerpendicularBisector() # print firstPoint # print lastPoint #print perpendiculaLine # print str(perpendiculaLine)+" zhuizhipingfen" nearestPoint = self.getNearestPointByLine(perpendiculaLine,self.pointList) l2 = line.Line() l3 = line.Line() #print str(firstPoint)+" d "+str(nearestPoint) l2.Line(firstPoint, nearestPoint);#nearestPoint must be at the second para l3.Line(lastPoint, nearestPoint);#nearestPoint must be at the second para intersectionAngle = l2.getLineIntersectionAngle(l3) ll = line.Line() ll.Line(firstPoint,lastPoint) radius = ll.getLength()/(2*math.sin(math.radians(intersectionAngle))) intersectionAngle = intersectionAngle/2 radiusAngle = 2*(180-intersectionAngle*2) #get X. Y. #get X. Y. #get X. Y. #get X. Y. midPoint = ll.getLineMidPoint() #error: midPoint and nearestPoint is same:bigger the midPoint if midPoint.X==nearestPoint.X and midPoint.Y==nearestPoint.Y: #print #error: midPoint and nearestPoint is same:bigger the midPoint midPoint.X = midPoint.X + midPoint.X*0.01 midPoint.Y = midPoint.Y + midPoint.Y*0.01 #error: midPoint and nearestPoint is same:bigger the midPoint smaDis = midPoint.getDistanceToOtherPoint(nearestPoint) #print "dff e e e "+str(midPoint)+" "+str(nearestPoint) xxx = nearestPoint.X-midPoint.X yyy = nearestPoint.Y-midPoint.Y #print "fdsf "+str(smaDis)+" "+str(radius) bili = radius/smaDis yuanxinX = nearestPoint.X-bili*xxx yuanxinY = nearestPoint.Y-bili*yyy return circle.circle(radiusAngle,radius,yuanxinX,yuanxinY,firstPoint,lastPoint)
def fitOneInscribedCircle(self,firstLine,secondLine): #find the nearest point to the waijieCircle(X0,Y0) # print "firstLine: "+str(firstLine) print str(firstLine.getLineLength())+" shifouxiangdeng "+str(secondLine.getLineLength()) firstSecondLineJiaoPoint = firstLine.getLineIntersectPoint(secondLine) interestPoint=[]#interestPoint must be on the firstLine interestPrePareOnFirstLinePoint=[] interestPrePareOnSecondLinePoint=[] biaoji ="" from flighttrackvector.ming.yu.wu.fitcircle import circle circlecircle = circle.circle() #check if have difficultPath,only if jiaoPoint is near Point2 of firstLine,and is near Point1 of secondLine #then make the Point1 of firstLine Link to Point2 of secondLine print str(firstLine.ifJiaodianPointOnThelineSegment(secondLine))+" dwfwf3rfwarfaw" if str(firstLine.ifJiaodianPointOnThelineSegment(secondLine))=="yes": biaoji="difficultPath" print "fdsfsefar3wrwar3war3war3awrawr3war3aw" return circlecircle,biaoji if firstLine.point1.getDistanceToOtherPoint(firstSecondLineJiaoPoint)>firstLine.point2.getDistanceToOtherPoint(firstSecondLineJiaoPoint) : interestPrePareOnFirstLinePoint = firstLine.point2 else: biaoji="difficultPath" return circlecircle,biaoji if secondLine.point1.getDistanceToOtherPoint(firstSecondLineJiaoPoint)>secondLine.point2.getDistanceToOtherPoint(firstSecondLineJiaoPoint) : biaoji="difficultPath" return circlecircle,biaoji else: interestPrePareOnSecondLinePoint = secondLine.point1 if interestPrePareOnFirstLinePoint.getDistanceToOtherPoint(firstSecondLineJiaoPoint) > interestPrePareOnSecondLinePoint.getDistanceToOtherPoint(firstSecondLineJiaoPoint): interestPoint = interestPrePareOnSecondLinePoint biaoji = "secondLineNear" else: interestPoint = interestPrePareOnFirstLinePoint print interestPoint biaoji = "firstLineNear" firstVector = firstLine.getUnitVector() secondVector = secondLine.getUnitVector() #find vector = [firstVector[0]+secondVector[0],firstVector[1]+secondVector[1]] k=vector[0]/vector[1] b=interestPoint.Y-k*interestPoint.X linshiX = 0.1010101#0.1010101 prevent chongfu y0=k*linshiX+b#linshi is optional from flighttrackvector.ming.yu.wu.fitline import line lll = line.Line() lll.Line(Point.Point(linshiX,y0), interestPoint) #check yuanxinPoint At Left OR Right from flighttrackvector.ming.yu.wu.fitline import line lll2 = line.Line() lll2.Line(firstLine.point2, secondLine.point1) if(biaoji=="firstLineNear"): symmetryPoint = secondLine.getLineIntersectPoint(lll)#duichenPoint firstVerticalLine = firstLine.getVerticalLineAtPoint(interestPoint) secondVerticalLine = secondLine.getVerticalLineAtPoint(symmetryPoint) yuanxinPoint = firstVerticalLine.getLineIntersectPoint(secondVerticalLine) radius = yuanxinPoint.getDistanceToOtherPoint(symmetryPoint) from flighttrackvector.ming.yu.wu.fitcircle import circle leftOrRight = lll2.pointAtVectorLeftOrRight(yuanxinPoint) circlecircle = circle.circle(firstVerticalLine.getLineIntersectionAngle(secondVerticalLine),radius,yuanxinPoint.X,yuanxinPoint.Y,interestPoint,symmetryPoint,leftOrRight) if(biaoji=="secondLineNear"): symmetryPoint = firstLine.getLineIntersectPoint(lll)#duichenPoint secondVerticalLine = secondLine.getVerticalLineAtPoint(interestPoint) firstVerticalLine = firstLine.getVerticalLineAtPoint(symmetryPoint) yuanxinPoint = firstVerticalLine.getLineIntersectPoint(secondVerticalLine) radius = yuanxinPoint.getDistanceToOtherPoint(symmetryPoint) from flighttrackvector.ming.yu.wu.fitcircle import circle leftOrRight = lll2.pointAtVectorLeftOrRight(yuanxinPoint) circlecircle = circle.circle(firstVerticalLine.getLineIntersectionAngle(secondVerticalLine),radius,yuanxinPoint.X,yuanxinPoint.Y,symmetryPoint,interestPoint,leftOrRight) return circlecircle,biaoji
return self.fitCircle.errorSumSquares(self.pointList),self.fitCircle; if __name__=='__main__': # pt1 = Point.Point(0,-1) # pt2 = Point.Point(1.414/2.0,1.414/2.0) # pt3 = Point.Point(13.02,153.04) # pt4 = Point.Point(-1.0,0) import math pt1 = Point.Point(-1.0,0.0) pt2 = Point.Point(-0.5,0.5) pt3 = Point.Point(0.25,0.75) pt4 = Point.Point(1.0,0.0) line1 = line.Line() line2 = line.Line() line1.Line(pt1, pt2) line2.Line(pt3,pt4) circle = circle.circle(X0=0.2,Y0=1) fitCircle = FitCircle([]) print fitCircle.fitOneInscribedCircle(line1,line2,circle) # print line1.getVerticalLineAtPoint(Point.Point(0,1.0)) # pppp = Point.Point(1,1) # nnLine = line.Line() # nnLine.Line(pt1, pt3) # ppppList=[pppp] # pointList = [pt1,pt2,pt3,pt4,pt5] # fitCircle = FitCircle(pointList) # fitCircle.errorSumSquares2()[1] #print fitCircle.errorSumSquares2(); print "fdsfs"
def mainFit(self,DorA,loadData,paodaoDuankou,flightNum,flightModel,lineError,circleError): startPointIndex = 0 import matplotlib.pyplot as plt # fig = plt.figure(figsize=(6,6)) # ax = plt.subplot(111) resultList = self.mainFit2(plt,lineError,circleError)#first Iteration # plt.plot([point.X for point in self.pointList],[point.Y for point in self.pointList],"black",alpha=0.8) print resultList import types for a in resultList: if(str(a).find("Line")!=-1): allPoint = a.getAllImagePointListFromXRange() print "zhixian "+str(a.point1)+" fdfds "+str(a.point2) # plt.plot([point.X for point in allPoint],[point.Y for point in allPoint],'o',color="green") else: allPoint = a.getAllImagePointListFromXRange() circle = Circle((a.X0,a.Y0),a.radius,alpha=0.25) # ax.add_patch(circle) print "111 "+str(resultList) #chu li you wen ti de hangxian difficultPath rangeLenResultListM2 =range(0,len(resultList)-2) for i in rangeLenResultListM2: if str(resultList[i]).find("Line")!=-1: fitCircle = FitCircle.FitCircle([]) circle,biaoji = fitCircle.fitOneInscribedCircle(resultList[i],resultList[i+2]) if biaoji.find("difficultPath")!=-1: from flighttrackvector.ming.yu.wu.fitcircle import circle lllLine = line.Line() lllLine.Line(resultList[i].point1,resultList[i+2].point2) resultList[i]=lllLine resultList[i+1]=circle.circle() resultList[i+2]=lllLine del resultList[i+2] del rangeLenResultListM2[-1] del resultList[i+1] del rangeLenResultListM2[-1] for i in rangeLenResultListM2: if str(resultList[i]).find("Line")!=-1: fitCircle = FitCircle.FitCircle([]) circle,biaoji = fitCircle.fitOneInscribedCircle(resultList[i],resultList[i+2]) if biaoji.find("difficultPath")!=-1: from flighttrackvector.ming.yu.wu.fitcircle import circle lllLine = line.Line() lllLine.Line(resultList[i].point1,resultList[i+2].point2) resultList[i]=lllLine resultList[i+1]=circle.circle() resultList[i+2]=lllLine del resultList[i+2] del rangeLenResultListM2[-1] del resultList[i+1] del rangeLenResultListM2[-1] for i in rangeLenResultListM2: if str(resultList[i]).find("Line")!=-1: fitCircle = FitCircle.FitCircle([]) circle,biaoji = fitCircle.fitOneInscribedCircle(resultList[i],resultList[i+2]) if biaoji.find("difficultPath")!=-1: from flighttrackvector.ming.yu.wu.fitcircle import circle lllLine = line.Line() lllLine.Line(resultList[i].point1,resultList[i+2].point2) resultList[i]=lllLine resultList[i+1]=circle.circle() resultList[i+2]=lllLine del resultList[i+2] del rangeLenResultListM2[-1] del resultList[i+1] del rangeLenResultListM2[-1] #waijieCircle change neiqieCircle for i in rangeLenResultListM2: print "resultList: "+str(i)+" "+str(resultList[i])+"shifou "+str(str(resultList[i]).find("Line")) if str(resultList[i]).find("Line")!=-1: list=[resultList[i],resultList[i+1],resultList[i+2]]#i is firstLine and i+2 is secondLine,i+1 is Circle fitCircle = FitCircle.FitCircle([]) circle,biaoji = fitCircle.fitOneInscribedCircle(resultList[i],resultList[i+2]) print "fdsfsdfesfes fsea fesa "+str(biaoji)+str(circle) # if biaoji.find("difficultPath")!=-1: # from flighttrackvector.ming.yu.wu.fitcircle import circle # lllLine = line.Line() # lllLine.Line(resultList[i].point1,resultList[i+2].point2) # resultList[i]=lllLine # resultList[i+1]=circle.circle() # resultList[i+2]=lllLine # del resultList[i+2] # del rangeLenResultListM2[-1] # del resultList[i+1] # del rangeLenResultListM2[-1] # print "ddddddfffffffdd"+ str(lllLine.point1)+str(lllLine.point2) # continue #discuss the position of circle.lastPoint with the secondLine resultList[i+1]=circle print str(biaoji)+" ddd "+str(biaoji.find("firstLineNear"))+ " dfdf33333333333333333333333d " if biaoji.find("firstLineNear")!=-1: lllLine=line.Line() lllLine.Line(resultList[i+1].lastPoint,resultList[i+2].point2) lllLine2 = line.Line() lllLine2.Line(resultList[i+2].point1,resultList[i+2].point2) print str(resultList[i+1].lastPoint)+" fdffdfdsdfsfdsafda "+str(resultList[i+2].point1) print str(lllLine) +" ffffffffff "+str(lllLine2) print str(resultList[i+1].lastPoint)+" fssssssssssssss " + str(resultList[i+2].point2) print str(resultList[i+2].point1)+" fssssssssssssss " + str(resultList[i+2].point2) print str(resultList[i+2])+" fsdddddddddddddd " + str(lllLine) resultList[i+2]=lllLine if biaoji.find("secondLineNear")!=-1: lll=line.Line() print "sssssssssdd "+str(i)+" "+str(resultList[i].point1)+" "+str(resultList[i+1].firstPoint) lll.Line(resultList[i].point1,resultList[i+1].firstPoint) resultList[i]=lll print "222 "+str(resultList) self.becauseDorAresultListChange(resultList,DorA,loadData,paodaoDuankou) #find #View the neijieCircle for a in resultList: if(str(a).find("Line")!=-1): print "Line "+str(a.point1)+" Line "+str(a.point2) allPoint = a.getAllImagePointListFromXRange() # plt.plot([point.X for point in allPoint],[point.Y for point in allPoint],"blue") # plt.plot(a.point1.X,a.point1.Y,"o",color = "green") # plt.plot(a.point2.X,a.point2.Y,"o",color = "green") else: # allPoint = a.getAllImagePointListFromXRange() print "Circle "+str(a.firstPoint)+" Circle "+str(a.lastPoint) circle = Circle((a.X0,a.Y0),a.radius,alpha=0.25,color="red") # ax.add_patch(circle) #runway bian de hang ji chu li print "s s s s" +str((resultList)) self.fitResultList = resultList runoutput = RunOutPut.RunOutPut(resultList) print runoutput.getNoisemapFormat(paodaoDuankou,flightNum,flightModel) return runoutput.getNoisemapFormat(paodaoDuankou,flightNum,flightModel)