Home

Chords on NXT


Chords is a simple program.
It computes n points within a circle,
then connects all the possible combinations.

This program is almost always the first thing I write when I get my hands on a new platform or language.  So when I discovered that LEGO NXT has a bitmap display, I wrote Chords for it.

I've never been a fan of programming by icon drag-n-drop, so I have not even bother to try the programming environment that came with the LEGO NXT. Instead, I played with NBC (Next Byte Code) by John Hansen. NBC is an assembler for the standard LEGO NXT firmware. I guess I have not seen assemblers recently, I was surprised to discover an expression evaluator capable of Sin and Cos. Unfortunately the evaluator runs at compile time so I had to hardcode the number of nodes.

 
//////////////////////////////////
// Chords by Hari Wiguna, 2006
// Illustrates pre-compiled Expression Evaluator (sin/cos)
// and bitmap graphics (DrawLine)
//////////////////////////////////

#include "NXTDefs.h"

// === Data Segment ===
dseg segment
   jMax byte 7 // 0 thru 7 = 8 nodes
   iMax byte // Upper limit for i loop
   i byte 0 // Outer loop variable
   j byte 0 // inner loop variable
   gX dword[] // X coordinate array
   gY dword[] // Y coordinate array
   dlArgs TDrawLine // DrawLine parameters
dseg ends

// === Code ===
thread main
   sub iMax, jMax, 1 // iMax is always one less than jMax
   
   arrinit gX, 0, jMax
   replace gX, gX, 0 , 50+sin(PI*2*0/7)*30
   replace gX, gX, 1 , 50+sin(PI*2*1/7)*30
   replace gX, gX, 2 , 50+sin(PI*2*2/7)*30
   replace gX, gX, 3 , 50+sin(PI*2*3/7)*30
   replace gX, gX, 4 , 50+sin(PI*2*4/7)*30
   replace gX, gX, 5 , 50+sin(PI*2*5/7)*30
   replace gX, gX, 6 , 50+sin(PI*2*6/7)*30

   arrinit gY, 0, jMax
   replace gY, gY, 0 , 30+cos(PI*2*0/7)*30
   replace gY, gY, 1 , 30+cos(PI*2*1/7)*30
   replace gY, gY, 2 , 30+cos(PI*2*2/7)*30
   replace gY, gY, 3 , 30+cos(PI*2*3/7)*30
   replace gY, gY, 4 , 30+cos(PI*2*4/7)*30
   replace gY, gY, 5 , 30+cos(PI*2*5/7)*30
   replace gY, gY, 6 , 30+cos(PI*2*6/7)*30

   iLoop:
      index dlArgs.StartLoc.X, gX, i
      index dlArgs.StartLoc.Y, gY, i
      add j,i, 1

      jLoop:
         index dlArgs.EndLoc.X, gX, j
         index dlArgs.EndLoc.Y, gY, j
         syscall DrawLine, dlArgs // Draws a line between nodes i and j
         add j,j, 1
      brcmp NEQ, jLoop, j, jMax // jump to jLoop if j <> jMax
	  
      add i,i, 1
   brcmp NEQ, iLoop, i, iMax // jump to iLoop if i <> iMax

   wait 4000
endt
Home