矢印を書いてみる
#!/usr/bin/ruby -Ke require "GD" require "iconv" class Arrow def initialize(len=100) a=GD::Polygon.new a.addPt(100,50) a.addPt(50,100) a.addPt(75,100) a.addPt(75,100+len) a.addPt(125,100+len) a.addPt(125,100) a.addPt(150,100) @arrow=a end def move(x=100,y=100) @arrow.offset(x,y) self end def scale(x=0.5,y=0.5) @arrow.scale(x,y) self end def rotate(x=10,y=10) # @arrow.transform(2,x,2,y,30,50) @arrow.transform(1,0.3,0.3,0.5,30,50) self end def to_p @arrow end end im = GD::Image.new(800,400) white = im.colorAllocate(255,255,255) black = im.colorAllocate(0,0,0) red = im.colorAllocate(255,0,0) blue = im.colorAllocate(0,0,255) im.polygon(Arrow.new(100).to_p,blue) im.polygon(Arrow.new(100).rotate.to_p,red) print "content-type: image/png\n\n" im.png STDOUT
回転の公式
座標、(x,y) にある点を、座標 (a,b) を中心に角度 r 回転すると、
x'= (x-a)cos(r)-(y-b)sin(r)+a
y'=(x-a)sin(r)+(y-b)cos(r)+b