# Line drawing with LineSegs

Hello,

I’m currently developing something which is supposed to draw lines according to simple commands. I’ve figured out how to draw lines so that’s not the problem. The problem is however to convert the commands to something panda is able to draw lines with. This is purely a 2D drawing project.

Example:

FWRD 20
TURN 90
FWRD 20
TURN 90
FWRD 20
TURN 90
FWRD 20

I assume you guessed that FWRD means forward and TURN means, well, to turn in degrees. I’ve been thinking about using vectors but I have no idea of how to go about it.

Do any of you have a suggestion or a better idea?

Well, an easy way would be to create a NodePath and move it according to the commands. Store the path, and then create a renderable geometry.

The script below uses this way. It created a 3D geometry, placed in the X/Y plane. The camera is places above the plane, looking down at it.

Since you said you know how to create lines I guess you are able to adapt the script to whatever you have in mind.

enn0x

``````from pandac.PandaModules import NodePath
from pandac.PandaModules import Point3
from pandac.PandaModules import Vec4
from pandac.PandaModules import LineSegs

class Writer:
def __init__( self ):
self.np = NodePath( 'pen' )
self.points = [ ]
self.reset( )

def forward( self, value ):
self.np.setPos( self.np, Point3( 0, value, 0 ) )
self.points.append( self.np.getPos( ) )

def turn( self, value ):
self.np.setH( self.np, value )

def reset( self ):
self.np.setPos( 0, 0, 0 )
self.points = [ ]
self.points.append( self.np.getPos( ) )

def create( self ):
segs = LineSegs( )
segs.setThickness( 2.0 )
segs.setColor( Vec4(1,1,0,1) )
segs.moveTo( self.points[0] )
for p in self.points[1:]: segs.drawTo( p )
return segs.create( )

o = Writer( )
o.forward( 20 )
o.turn( 90 )
o.forward( 20 )
o.turn( 90 )
o.forward( 20 )
o.turn( 90 )
o.forward( 20 )

import direct.directbase.DirectStart
render.attachNewNode( o.create( ) )
base.disableMouse( )
base.camera.setPos( 0, 0, 100 )
base.camera.lookAt( 0, 0, 0 )
run( )``````

This was exactly what I was looking for! Thank you very much. Never knew it would be so simple. I was messing around with points/vectors and calculations