Draw Image lines (AA lines for example)

Monkey Forums/Monkey Code/Draw Image lines (AA lines for example)

JIM(Posted 2011) [#1]
Here's a piece of code that you can use to replace lines with stretched images.

One particular use would be drawing an Anti-Aliased line. You can also control the width of the line by changing the image:

Function DrawImageLine(img:Image, x1#, y1#, x2#, y2#)
	PushMatrix()
	
	Local Angle# = ATan2(x2 - x1, y2 - y1)
	Local Size# = Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))

	Translate(x1, y1)
	Rotate(270 +  Angle)
	Scale(Size / img.Width(), 1.0)
	DrawImage(img, 0, 0)
	
	PopMatrix()
End


It requires a horizontal image with the handle set to the left side.
Ex.:
If you want the image centered, SetHandle(0, img.Height() / 2.0).
If you want the image to the left of the line, SetHandle(0, img.Height()).


Chaduke(Posted 2011) [#2]
I've never seen this technique used before but it works great. I found that when I created the line image I had to leave 1 pixel width of transparency on the top and the bottom in order to get the AA effect.


JIM(Posted 2011) [#3]
Ah yes, I forgot to mention that. My image is 1x3, with the middle pixel being black and the other two invisible :)