Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:51

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 
0023 http://www.hannahfry.co.uk/blog/2011/11/16/bezier-curves
0024 
0025 """
0026 
0027 import scipy as sp
0028 import pylab as plt
0029 
0030 
0031 plt.ion()
0032 plt.close()
0033 
0034 #### Inputs
0035 
0036 #A list of P0's and P3's. Must be the same length
0037 
0038 origins = [
0039              [1,0],
0040              [-0.5, sp.sqrt(3)/2], 
0041              [-0.5,-sp.sqrt(3)/2]
0042           ]
0043 
0044 destinations = [[0,0],[0,0],[0,0]]
0045 
0046 #The angle the control point will make with the green line
0047 blue_angle = sp.pi/6
0048 red_angle = sp.pi/4
0049 
0050 #And the lengths of the lines (as a fraction of the length of the green one)
0051 blue_len = 1./5
0052 red_len = 1./3
0053 
0054 ### Workings
0055 
0056 #Generate the figure
0057 fig = plt.figure()
0058 ax = fig.add_subplot(111)
0059 ax.hold(True)
0060 
0061 #Setup the parameterisation
0062 t = sp.linspace(0,1,100)
0063 
0064 #Read in the origin & destination points
0065 for i in xrange(len(origins)):
0066     POx,POy = origins[i][0], origins[i][1]
0067     P3x,P3y = destinations[i][0], destinations[i][1]
0068 
0069 #Add those to the axes
0070     ax.plot(POx,POy, 'ob')
0071     ax.plot(P3x,P3y, 'or')
0072     ax.plot((POx,P3x),(POy,P3y), 'g')
0073 
0074 #Work out r and theta (as if based at P3)
0075     r = ((POx-P3x)**2 + (POy-P3y)**2)**0.5
0076     theta = sp.arctan2((POy-P3y),(POx-P3x))
0077 
0078 #Find the relevant angles for the control points
0079     aO =theta + blue_angle+ sp.pi
0080     aD = theta - red_angle
0081 
0082 #Work out the control points
0083     P1x, P1y = POx+ blue_len*r*sp.cos(aO), POy + blue_len*r*sp.sin(aO)
0084     P2x, P2y = P3x+ red_len*r*sp.cos(aD), P3y + red_len*r*sp.sin(aD)
0085 
0086 #Plot the control points and their vectors
0087     ax.plot((P3x,P2x),(P3y,P2y), 'r')
0088     ax.plot((POx,P1x),(POy,P1y), 'b')
0089     ax.plot(P1x, P1y, 'ob')
0090     ax.plot(P2x, P2y, 'or')
0091 
0092 #Use the Bezier formula
0093     Bx = (1-t)**3*POx + 3*(1-t)**2*t*P1x + 3*(1-t)*t**2*P2x + t**3*P3x
0094     By = (1-t)**3*POy + 3*(1-t)**2*t*P1y + 3*(1-t)*t**2*P2y + t**3*P3y
0095 
0096 #Plot the Bezier curve
0097     ax.plot(Bx, By, 'k')
0098 pass
0099 
0100 #Save it
0101 #plt.savefig('totally-awesome-bezier.png')
0102 plt.show()
0103 #Bosch.