cubicsuperpath.py 5.12 KB
 Ted Gould committed May 24, 2005 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ``````#!/usr/bin/env python """ cubicsuperpath.py Copyright (C) 2005 Aaron Spike, aaron@ekips.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software `````` Nicolas Dufour committed Mar 16, 2016 19 ``````Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. `````` Ted Gould committed May 24, 2005 20 21 22 `````` """ import simplepath `````` Aaron Spike committed Nov 23, 2006 23 ``````from math import * `````` Ted Gould committed May 24, 2005 24 `````` `````` Aaron Spike committed Nov 23, 2006 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 ``````def matprod(mlist): prod=mlist[0] for m in mlist[1:]: a00=prod[0][0]*m[0][0]+prod[0][1]*m[1][0] a01=prod[0][0]*m[0][1]+prod[0][1]*m[1][1] a10=prod[1][0]*m[0][0]+prod[1][1]*m[1][0] a11=prod[1][0]*m[0][1]+prod[1][1]*m[1][1] prod=[[a00,a01],[a10,a11]] return prod def rotmat(teta): return [[cos(teta),-sin(teta)],[sin(teta),cos(teta)]] def applymat(mat, pt): x=mat[0][0]*pt[0]+mat[0][1]*pt[1] y=mat[1][0]*pt[0]+mat[1][1]*pt[1] pt[0]=x pt[1]=y def norm(pt): return sqrt(pt[0]*pt[0]+pt[1]*pt[1]) def ArcToPath(p1,params): A=p1[:] rx,ry,teta,longflag,sweepflag,x2,y2=params[:] `````` Alvin Penner committed Feb 08, 2009 47 `````` teta = teta*pi/180.0 `````` Aaron Spike committed Nov 23, 2006 48 `````` B=[x2,y2] `````` Alvin Penner committed Nov 11, 2013 49 50 `````` if rx==0 or ry==0 or A==B: return([[A[:],A[:],A[:]],[B[:],B[:],B[:]]]) `````` Aaron Spike committed Nov 23, 2006 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 `````` mat=matprod((rotmat(teta),[[1/rx,0],[0,1/ry]],rotmat(-teta))) applymat(mat, A) applymat(mat, B) k=[-(B[1]-A[1]),B[0]-A[0]] d=k[0]*k[0]+k[1]*k[1] k[0]/=sqrt(d) k[1]/=sqrt(d) d=sqrt(max(0,1-d/4)) if longflag==sweepflag: d*=-1 O=[(B[0]+A[0])/2+d*k[0],(B[1]+A[1])/2+d*k[1]] OA=[A[0]-O[0],A[1]-O[1]] OB=[B[0]-O[0],B[1]-O[1]] start=acos(OA[0]/norm(OA)) if OA[1]<0: start*=-1 end=acos(OB[0]/norm(OB)) if OB[1]<0: end*=-1 if sweepflag and start>end: end +=2*pi if (not sweepflag) and start