#---------------------------------------------------------------------- # Routines that support computational geomtry. # # Copyright (C) April 3, 2017 -- Dr. William T. Verts #---------------------------------------------------------------------- #---------------------------------------------------------------------- # Let's start with something simple: the distance between two points. # From Pythagoras, it is the square root of the sum of the squares # of the difference in X and the difference in Y. Explicitly passing # the coordinates of and to the function gives us the # traditional approach: #---------------------------------------------------------------------- def DistanceOLD (X1,Y1,X2,Y2): DeltaX = X2 - X1 DeltaY = Y2 - Y1 return sqrt(DeltaX*DeltaX + DeltaY*DeltaY) #---------------------------------------------------------------------- # This has several problems. First is that it is not general to any # number of dimensions, and second is that points have no defined # existence other than coincidental use of X and Y. # # So, we will define a "point" as a list of coordinate values. # 2D points would be [X,Y]. That is, X is at location [0], and # Y is at location [1] in the list. For 3D points, each list has # three values. For 4D points, each list has four values. #---------------------------------------------------------------------- def Distance2D (P1,P2): # Both P1 and P2 are [X,Y] points DeltaX = P2[0] - P1[0] DeltaY = P2[1] - P1[1] return sqrt(DeltaX*DeltaX + DeltaY*DeltaY) def Distance3D (P1,P2): # Both P1 and P2 are [X,Y,Z] points DeltaX = P2[0] - P1[0] DeltaY = P2[1] - P1[1] DeltaZ = P2[2] - P1[2] return sqrt(DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ) def Distance4D (P1,P2): # Both P1 and P2 are [X,Y,Z,W] points DeltaX = P2[0] - P1[0] DeltaY = P2[1] - P1[1] DeltaZ = P2[2] - P1[2] DeltaW = P2[3] - P1[3] return sqrt(DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ + DeltaW*DeltaW) #---------------------------------------------------------------------- # We still haven't properly addressed the problem of different # dimensions. We wrote separate functions for 2D, 3D, and 4D. # Instead, we want to have the lists represent points in an arbitrary # number of dimensions, denoted by the length of the lists that form # P1 and P2. Unfortunately, there is no guarantee that the user won't # give two points with different dimensions; our solution is to write # the distance function to use the smaller number of dimensions. This # function is now general, as it is agnostic to the number of # dimensions. It will work equally well on 2D, 3D, 4D, 5D, 6D, 7D, ... # points. #---------------------------------------------------------------------- def Distance (P1,P2): # Both P1 and P2 are arbitrary dimensional points Total = 0 for I in range(min(len(P1),len(P2))): Delta = (P2[I] - P1[I]) Total = Total + Delta * Delta return sqrt(Total) #---------------------------------------------------------------------- # Let's say that we have two values, N0 and N1, as well as a # "parameter" that controls how fars between N0 and N1 we want to be. # When the parameter T=0 we want to be at N0, when T=1 we want # to be at N1, when 01, which linearly extrapolates values outside # the N0...N1 range. #---------------------------------------------------------------------- def Blend (N0,N1,T): return (N1 - N0) * float(T) + N0 #---------------------------------------------------------------------- # We use Blend to compute points intermediate between two endpoints on # a line. P0 and P1 are points (in any dimension), T is the "knob" to # twist. We are at P0 on the line when T=0, at P1 when T=1, and in # between those points when 0