Skip to content

Instantly share code, notes, and snippets.

@tompng
Created June 2, 2020 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompng/c573125e59d18413ef7fb08f3677f11c to your computer and use it in GitHub Desktop.
Save tompng/c573125e59d18413ef7fb08f3677f11c to your computer and use it in GitHub Desktop.
extract xy from svg path
def hoge s
hoges = s.scan(/[A-Za-z]|-?\d*\.\d*|\d+/).map{|a|a =~ /[\d.]/ ? a.to_f : a}
p hoges
x=y=0
out=[]
while hoges.first
begin
c = hoges.shift
case c
when 'M'
x, y = hoges.shift(2)
out.push p [x, y]
when 'C', 'S'
hoges.shift(c == 'C' ? 4 : 2)
x, y = hoges.shift(2)
out.push p [x, y]
when 'c', 's'
while hoges[0].is_a? Numeric
hoges.shift(c == 'c' ? 4 : 2)
dx, dy = hoges.shift(2)
x += dx
y += dy
out.push p [x, y]
end
when 'a'
while hoges[0].is_a? Numeric
hoges.shift(5)
dx, dy = hoges.shift(2)
x += dx
y += dy
out.push p [x, y]
end
when 'z'
else
raise c.to_s
end
rescue => e;binding.irb;end
end
puts out.map{|a|'L'+a.join(',')}.join.sub('L', 'M')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment