# -*- coding: utf-8 -*- """ Created on Mon Feb 19 17:59:31 2018 @author: noordam """ import math epsilon =1e-17 #============================================================================== # compute the intersection between a line and a circle #============================================================================== def computeCircleLineIntersection(xA,yA,xB, yB, xC, yC, r): # compute the euclidean distance between A and B lAB =math.sqrt((xB-xA)**2.0+(yB-yA)**2.0); # compute the direction vector D from A to B dX = (xB - xA) /lAB; dY = (yB - yA) /lAB; # Now the line equation is x = Dx*t + Ax, y = Dy*t + Ay with 0 <= t <= 1. # compute the value t of the closest point to the circle center (Cx, Cy) t = dX*(xC -xA) + dY*(yC-yA); # This is the projection of C on the line from A to B. # compute the coordinates of the point E on line and closest to C xE = t*dX+xA; yE = t*dY + yA; # compute the euclidean distance from E to C lEC = math.sqrt((xE-xC)**2.0+(yE-yC)**2.0); # test if the line intersects the circle if lEC < r: # compute distance from t to circle intersection point dt = math.sqrt( r**2.0 - lEC**2.0) # compute first intersection point # xF = (t-dt)*dX + xA # yF = (t-dt)*dY + yA # compute second intersection point xG = (t+dt)*dX + xA yG = (t+dt)*dY + yA # print xF # print yF if xG> xA and xG<=xB: return(xG,yG) else: return False # else test if the line is tangent to circle elif( lEC == r ): # tangent point to circle is E print ("only 1 intersection") return False else: # line doesn't touch circle print ("no intersections") return False #============================================================================== # get the part of the surface line on which x is located #============================================================================== def GetNearestLineAtX(x,xCoordsSurfaceLine,yCoordsSurfaceLine): ii =0; if x>=xCoordsSurfaceLine[len(xCoordsSurfaceLine)-1]: return False xLinePoint1=0.0; while x> xCoordsSurfaceLine[ii] and iixCoordsSurfaceLine[ii] and ii=xStart-epsilon: indexesInRange.extend([ii]) ii=ii+1; else: while ii=xStart-epsilon and xEnd>xCoordsSurfaceLine[ii]: indexesInRange.extend([ii]) ii=ii+1; return indexesInRange