Log in

View Full Version : Drawing lines with Gscript


tin3tin
28th November 2010, 16:45
Here's a function for drawing lines with Gscript:

Loadplugin("GScript.dll")
blankclip(1,500,500,"RGB32", color=$FF0000FF)

# Example a line:
#DrawLine(canvas, 1,1,500,500, $FFFFFFFF)

#Example a red box:
#DrawLine(last, 100,100,400,100,5, $FFFF0000)
#DrawLine(last, 100,100,100,400,5, $FFFF0000)
#DrawLine(last, 100,400,400,400,5, $FFFF0000)
#DrawLine(last, 400,100,400,400,5, $FFFF0000)

# Example a lot of lines:
GScript("""
for (i=0, 500,10){
DrawLine(last, 0,i,500,500-i,1, $FFFFFFFF)
DrawLine(last, i,0,500-i,500,1, $FFFFFFFF)
}

""")

function DrawLine(clip canvas, val x0, val y0,val x1,val y1, val dsize, val dcolor)
{
dtotal=canvas.framecount()
OptCanvas=blankclip(1,canvas.width(),canvas.height(),"RGB32", color=$00000000)
pen=blankclip(1,dsize,dsize,"RGB32", color=dcolor)

dx = float(x1 - x0)
dy = float(y1 - y0)

sx = dx > 0 ? 1 : -1
sy = dy > 0 ? 1 : -1

dx = float(dx*sx)
dy = float(dy*sy)

dxdy= float(dx/dy)
dydx = float(dy/dx)

dxdy = dxdy *sx
dydx = dydx *sy

py = float(y0)
px = float(x0)

GScript("""
if( dx > dy )
{

for(x=0, int(dx))
{
OptCanvas=Layer(OptCanvas,pen,"add",257,int(px),int(py))
px=px+sx
py=py+dydx
}
}
else
{
for(y=0, int(dy))
{
OptCanvas=Layer(OptCanvas,pen,"add",257,int(px),int(py))
py=py+sy
px=px+dxdy
}
}

""")
return Layer(Canvas,OptCanvas.loop(dtotal),"add",257,0,0)
}

tin3tin
28th November 2010, 19:13
Tried to make a DrawCircle too, from this example: http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html

However there is an error... :(

[Edit: Thanks Gavino! here's the working script:]

Loadplugin("GScript.dll")
blankclip(1,500,500,"RGB32", color=$FF0000FF)

DrawCircle(last, 250,250,100, 2, $FFFFFFFF)

function DrawCircle(clip canvas, val CenterX, val CenterY,val radius, val pensize, val pencolor)
{
dtotal=canvas.framecount()
OptCanvas=blankclip(1,canvas.width(),canvas.height(),"RGB32", color=$00000000)
pen=blankclip(1,pensize,pensize,"RGB32", color=pencolor)
GScript("""
x = 0
y = radius
p = (5 - radius*4)/4

while (x < y)
{
x=x+1
if (p < 0) {
p = p+2*x+1
} else {
y=y-1
p =p+ 2*(x-y+1)
}
OptCanvas=circlePoints(CenterX, CenterY, x, y, OptCanvas, pen)
OptCanvas=Layer(OptCanvas,pen,"add",257,CenterX+int(x),CenterY+int(y))
}
""")

return Layer(Canvas,OptCanvas.loop(dtotal),"add",257,0,0)
}

function circlePoints(int cx, int cy, int x, int y, clip optCanvas, clip pen)
{

GScript("""
if (x == 0) {
OptCanvas=Layer(OptCanvas,pen,"add",257, cx, cy + y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx, cy - y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx + y, cy)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - y, cy)
} else if (x == y) {
OptCanvas=Layer(OptCanvas,pen,"add",257,cx + x, cy + y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy + y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx + x, cy - y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy - y)
} else if (x < y) {
OptCanvas=Layer(OptCanvas,pen,"add",257, cx + x, cy + y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy + y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx + x, cy - y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy - y)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx + y, cy + x)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - y, cy + x)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx + y, cy - x)
OptCanvas=Layer(OptCanvas,pen,"add",257, cx - y, cy - x)
}
""")
return OptCanvas
}

Gavino
28th November 2010, 21:56
function DrawCircle( ... )
{
...
optCanvas = circlePoints(CenterX, CenterY, x, y, OptCanvas, pen)
...
}

function circlePoints( ... )
{
...
if (x == 0) {
...
} else if (x == y) {
...
}

tin3tin
28th November 2010, 22:09
Thanks Gavino! :)

I was so close... and so blind...

Jenyok
17th March 2013, 05:50
tin3tin
.
Exuse me, mate.
.
How to draw ARC ?
How to draw ELLIPSE ?
.
Gavino
.
Can you help, mate ?
.

tin3tin
17th March 2013, 08:42
What I did was to google up some code handling the math and then changed it to Avisynth script.

StainlessS
17th March 2013, 13:20
Maybe of interest:
http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
http://en.wikipedia.org/wiki/Ellipse
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
http://en.wikipedia.org/wiki/Digital_Differential_Analyzer_%28graphics_algorithm%29

Jenyok
17th March 2013, 14:58
StainlessS
Thanks, mate.
.
.
I do not understand, why line of code does not works, see code below ?
.

LoadPlugin("C:\PROGRAM FILES\AVISYNTH 2.5\PLUGINS\GSCRIPT_11\gscript.dll")

function DrawCircle(clip canvas, val CenterX, val CenterY, val radius, val pensize, val pencolor)
{
dtotal = canvas.framecount()
OptCanvas = blankclip(1, canvas.width(), canvas.height(), "RGB32", color=$00000000)
pen = blankclip(1, pensize, pensize, "RGB32", color=pencolor)

GScript("""
x = 0
y = radius
p = (5 - radius * 4) / 4

dt = 0

while (x < y) {
x = x + 1

if (p < 0) {
p = p + 2 * x + 1
}
else {
y = y - 1
p = p + 2 * (x - y + 1)
}

OptCanvas = circlePoints(CenterX, CenterY, x, y, OptCanvas, pen)
OptCanvas = Layer(OptCanvas, pen, "add", 257, CenterX+int(x), CenterY+int(y))

OptCanvas = SubTitle(OptCanvas, "p = "+string(p), x=5, y=5+dt, size=10, align=1, text_color=$FFFFFF, halo_color=$000000, font="Arial Cyr") # Why does this line not works ?
dt=dt+10
}
""")

return (Layer(canvas, OptCanvas.loop(dtotal), "add", 257, 0, 0))
}


function circlePoints(int cx, int cy, int x, int y, clip optCanvas, clip pen)
{
GScript("""
if (x == 0) {
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx, cy + y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx, cy - y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + y, cy)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - y, cy)
}
else if (x == y) {
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy + y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy + y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy - y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy - y)
}
else if (x < y) {
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy + y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy + y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy - y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy - y)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + y, cy + x)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - y, cy + x)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + y, cy - x)
OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - y, cy - x)
}
""")

return (OptCanvas)
}



BlankClip(length=50, width=768, height=576, fps=25.0, channels=2, color=$000000)

x2 = Width() / 2
y2 = Height() / 2

DrawCircle(last, x2+200, y2, 100, 2, $FFFFFFFF)

ConvertToYUY2()

Gavino
17th March 2013, 21:07
I do not understand, why line of code does not works, see code below ?
It's because OptCanvas is created transparent (alpha=0), and Subtitle() does not change the alpha layer.
You need to add this line after the Subtitle call:
OptCanvas = OptCanvas.Mask(OptCanvas.ShowAlpha().SubTitle("p = "+string(p), x=5, y=5+dt, size=10, align=1, text_color=$FFFFFF, halo_color=$FFFFFF, font="Arial Cyr"))
This will set alpha to 255 (opaque) where Subtitle() has written the text.

Jenyok
18th March 2013, 06:34
Gavino
.
Thanks, mate.
It works.
.