Let's Make Robots!

Processing Help -Draw line at given angle

If someone could point me in the right direction here -or- maybe a link to some code examples.

I would like to draw a line from a fixed point, a given length and at a given angle. I have found some examples that show a line that will follow your mouse and will rotate around a fixed point, but not one including length.

Here is what I am looking at doing:

I want to draw a map (just a line showing a path) on my screen as walter travels around. Each time walter changes direction, a little data packet is sent describing his last move. I.e.

fwd (16) counts (send that to processing and a line is drawn from starting point and is (16) units long)

rotated (4) counts or 1/4 turn (send to processing and the angle of the next line is determined)

fwd (27) counts (a line, 27 units long is drawn at the angle determined above)

Each new line segment will start from the end of the last one drawn.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

No problem. Glad I could help. I took a deeper look how that serial lib works in Processing and found out it supports events. So instead of checking if there's something available in serial port every now and then (or every draw() call like in my previous post) you can use serialEvent method call to catch an event when there's something available. Could be useful in your app if you don't already use it (check also bufferUntil). Here's something I put together (sorry again for a long post):
import processing.serial.*;

PGraphics buffer;
PImage img;
Serial PicPort;
String myString = null;
int cr = 13; // strings are terminated by ascii 13: carriage return aka newline
float oldangle = 0;
float x0 = 0;
float y0 = 0;
float x1 = 0;
float y1 = 0;
float angle = 0;
float travel = 0;

void setup() {
  //                 parent comport           baud  parity databits stopbit
  PicPort = new Serial(this, Serial.list()[0], 4800, 'N',   8,       1.0);
  PicPort.bufferUntil(cr); // Buffer until CR is found (before calling serialEvent)
  // Create an off-screen buffer and initial image.
  buffer = createGraphics(600, 400, JAVA2D);
  buffer.translate(width / 2, height / 2);
  buffer.ellipse(0,0 , 10,10);
  img = buffer.get(0, 0, buffer.width, buffer.height);

void draw() {
  translate(width / 2, height / 2);
  image(img, -width/2, -height/2);

// This gets called whenever there's data available
void serialEvent(Serial p) {
  myString = p.readStringUntil(cr);
  if (myString != null) {
    // println(myString);
    // examples of myString: "r:259", "t:18", "t:0"
    String telemetry [] = split(myString, ":");
    if (telemetry.length == 2) {
      String qualifier = telemetry[0];
      float value = float(telemetry[1]);  //  WHY won't this convert to int ?!?!
      int valint = int(value);

      //     println (qualifier +"="+ valint);

      if (qualifier.equals("r")) {
        // rotation
        angle = oldangle + value / 360 * TWO_PI; // input ranges 0-359, result is in radians
        //      println ("rotated: " + value + " degrees" +"   "+ angle +" radians");
      if (qualifier.equals("t")) {
        // travel
        travel = value / 50 * width * .01;  // input ranges 0-50, result is in 1% increments of screen width
        //     println ("traveled: " + value + " mickeys" +"    "+ travel +" pixels");

        //     background(192);
        x1 = x0 + travel * cos(angle);
        y1 = y0 + travel * sin(angle);
        buffer.translate(width / 2, height / 2);
        buffer.line(x0,y0 , x1, y1);
        oldangle = angle;
        x0 = x1;
        y0 = y1;

  img = buffer.get(0, 0, buffer.width, buffer.height);

I noticed that sometimes the sketch freezes like there's nothing coming out from serial port even when there is. I'm not sure if it's a problem with sketch itself or the underlying library. Serial library seems to use RXTX and I remember having some problems with it a few years back (although it could have been my mistake then too).


You know rik, I have been playing with the code and to be honest, I just don't want to bother converting to a 256 circle. It is simply worth using a word to get the 359 that I need. In reality, most of the time I would not need anything past 270. I tried sending a - along with my travel distance (r:0) and sure enough, it back-tracked along it's own line. Perfect.

You know, I thought I was done with hardware on Walter and here I am adding 2 more encoders and one more picaxe. Not to mention I just added a RFID reader and the new voice box.

That is why the Good Lord made min and max, my friend.

As we all know, picaxe does not like negitive numbers. I would say that I will have to find a mid-point between a given min and max and have processing translate that mid-point to 0 and thus go positive and negitive from there. To be honest, it was very late last night when I was playing with your code so I didn't have a lot of time to figure out numbers. I have a lot of chores to do today, but if I get them done and get to play with my computer, I have some test plans in the works:

I was figuring on two pots with a debug to sertxd or LCD for turns and distance. A push button would send the data after it is dialed in by the pot. This way I can see exactly what I am sending and what I am getting. I'll probably write all that down on a piece of paper using a pencil. From there, I just need to find my notes on my encoder count math and do the conversion. Should be pretty simple. Not to mention, one of my chores today is to get this house cleaned up --which is needed for walter to go "free range"... There are a couple "sonar holes" that need a piece of cardboard in front of them and a big sonar-soaking couch which appears as infinity and is thus a walter-magnet. I should have some solid working results soon.

Word to ya motha'

The picaxe does not NEED to understand negatives, as long as it is transmitting the "-" character when appropriate. But then of course, who needs it: 30 degrees CCW = 330 degrees CW anyways.

The real fun starts with using a circle made of degrees 0-255, so that it will always fit inside a byte. You might want to make up a silly, distinctive name for that unit. Wikipedia probably already has it on file. Without the funny.

Another advantage of the single byte approach to headings/bearings: the roll-over at the bottom and top of the range:
255 + 1 = 256 = 0
2 - 3 = -1 = 255

is called a "brad" (binary rad).