Skip to content

Commit 43ad106

Browse files
committed
directionalize
1 parent f4f4796 commit 43ad106

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

gcodeplot.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,39 @@ def getConfigOpts(filename):
596596
opts.append( (opt,arg) )
597597
return opts
598598

599+
def directionalize(paths, angle):
600+
vector = (math.cos(angle * math.pi / 180.), math.sin(angle * math.pi / 180.))
601+
602+
outPaths = []
603+
for path in paths:
604+
startIndex = 0
605+
prevPoint = path[0]
606+
canBeForward = True
607+
canBeReversed = True
608+
i = 1
609+
while i < len(path):
610+
curVector = (path[i][0]-prevPoint[0],path[i][1]-prevPoint[1])
611+
if curVector[0] or curVector[1]:
612+
dotProduct = curVector[0]*vector[0] + curVector[1]*vector[1]
613+
if dotProduct > 0 and not canBeForward:
614+
outPaths.append(list(reversed(path[startIndex:i])))
615+
startIndex = i-1
616+
canBeForward = True
617+
canBeReversed = False
618+
elif dotProduct < 0 and not canBeReversed:
619+
outPaths.append(path[startIndex:i])
620+
startIndex = i-1
621+
canBeForward = False
622+
canBeReversed = True
623+
prevPoint = path[i]
624+
i += 1
625+
if canBeForward:
626+
outPaths.append(path[startIndex:i])
627+
else:
628+
outPaths.append(list(reversed(path[startIndex:i])))
629+
630+
return outPaths
631+
599632
if __name__ == '__main__':
600633

601634
def help(error=False):
@@ -980,7 +1013,7 @@ def help(error=False):
9801013
for pen in penData:
9811014
penData[pen] = op.processPath(penData[pen])
9821015

983-
if optimizationTime > 0.:
1016+
if optimizationTime > 0. and directionAngle is None:
9841017
for pen in penData:
9851018
penData[pen] = anneal.optimize(penData[pen], timeout=optimizationTime/2., quiet=quiet)
9861019
penData = removePenBob(penData)
@@ -990,6 +1023,11 @@ def help(error=False):
9901023
penData[pen] = safeSorted(penData[pen], comparison=comparePaths)
9911024
penData = removePenBob(penData)
9921025

1026+
if directionAngle is not None:
1027+
for pen in penData:
1028+
penData[pen] = directionalize(penData[pen], directionAngle)
1029+
penData = removePenBob(penData)
1030+
9931031
if len(penData) > 1:
9941032
sys.stderr.write("Uses the following pens:\n")
9951033
for pen in sorted(penData):

svgpath/shader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def shade(self, polygon, grayscale, avoidOutline=True, mode=None):
3636
spacing = self.lightestSpacing * (1-intensity) + self.darkestSpacing * intensity
3737
lines = Shader.shadePolygon(polygon, self.angle, spacing, avoidOutline=avoidOutline, mode=mode)
3838
if self.crossHatch:
39-
lines += Shader.shadePolygon(polygon, self.angle+90, spacing, avoidOutline=avoidOutline, mode=mode, alternate=(self.drawingDirectionAngle==None))
39+
lines += Shader.shadePolygon(polygon, self.angle+90, spacing, avoidOutline=avoidOutline, mode=mode, alternate=(self.drawingDirectionAngle is None))
4040
return lines
4141

4242
@staticmethod

0 commit comments

Comments
 (0)