Loading...
Logo
Processing Forum

Triangle normal

in Programming Questions  •  2 years ago  
Hi guys,
I am trying to understand what is wrong with the cross product. It is giving me some funky result when I normalize it. I guess its a simple one...really thankful if somebody could help. Here is the code:
Thanks a lot :)
buzz

// peasyCam library
import peasy.*;
// orbit
PeasyCam cam;

void setup()
{
  size(600, 600, P3D);
  smooth();
  colorMode(HSB, 6, 100, 100); // mode, rangeHue, rangeSat, rangeBright

  cam = new PeasyCam(this, 100); // qto maior menor o obj
  cam.setMinimumDistance(10); // close zoom
  cam.setMaximumDistance(10000); // far zoom
}

void draw()
{
  background(6);
  //translate(width/2, height/2, 0);
  //rotateY(frameCount*0.01);

  PVector a =  new PVector(100, 100, 0);
  PVector b =  new PVector(150, 100, 0);
  PVector c =  new PVector(200, 200, 0);
  strokeWeight(5);
  stroke(0);
  point(a.x, a.y, a.z);
  point(b.x, b.y, b.z);
  point(c.x, c.y, c.z);

  // 1. calculate the cross product
//  PVector n = new PVector((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x));
//  stroke(0, 100, 100);
//  strokeWeight(5);
//  point(n.x, n.y, n.z);

  // 1.1 cross product check
  PVector n = a.cross(b);
  //n.normalize(); ?????????????????????????????????
  println(n);
  stroke(0, 100, 100);
  strokeWeight(15);
  point(n.x, n.y, n.z);

  // 2. find the centroid
  PVector g = new PVector( (a.x+b.x+c.x)/3, (a.y+b.y+c.y)/3, (a.z+b.z+c.z)/3 );
  stroke(2, 100, 100);     
  strokeWeight(5);
  point(g.x, g.y, g.z);
 
  float d = g.dist(n);
  println(d); 

  // 3. draw the normal
  strokeWeight(1);
  stroke(0, 100, 100);
//  line(g.x, g.y, g.z, n.x, n.y, n.z);
  line(g.x, g.y, g.z, n.x, n.y, n.z);

  // 4. pvector that follows the mouse - will be replaced by the sun
  PVector m = new PVector(mouseX, mouseY, 300);
  stroke(0);
  line(g.x, g.y, g.z, m.x, m.y, m.z); 

  // 5. calcullate the angle between sun and normal vector
  float angle = PVector.angleBetween(n, m);
  //println(degrees(angle));

  // 6. map the angle to a color value anf fill the triangle to that value
  float mapping = map(angle, 0, 180, 0, 255); //possible angles to possible colours
  //println(mapping);
  strokeWeight(1);
  stroke(0);
  fill(mapping, 100, 100);
  beginShape(TRIANGLES);
  vertex(a.x, a.y, a.z);
  vertex(b.x, b.y, b.z);
  vertex(c.x, c.y, c.z);
  endShape(); 
}

Replies(2)

Re: Triangle normal

2 years ago
"some funky result"

these are the kind of bug reports that we all love! funky in what way? what are you expecting? what are you actually seeing?

n.normalize will reduce the length of the supplied vector to 1. might be hard to spot amongst all the other lines.

(even harder if you're using point() to display it)

think this is your problem though - this draws a line from centroid to a point very near the origin.
  line(g.x, g.y, g.z, n.x, n.y, n.z);

you need to add the centroid to those last 3 points and multiple the normal so it's not length 1, just for visibility. something like this.
  line(g.x, g.y, g.z, g.x + 30 * n.x, g.y + 30 * n.y, g.z + 30 * n.z);

(sorry, don't have peasycam here so this is largely guesswork)
Kooogy,
Thanks a lot for your response. It helped.
I meant wrong when I wrote funky, what I was expecting was to see a vector perpendicular to the plane that the other vectors belong to and what I was seeing was a vector towards the origin.
I didn't understand why it was drawing the line towards the origin and that I had to add the centroid to the normal.
buzz