Loading...
Logo
Processing Forum
Hi everyone, 

I am just starting with Processing ( I am a girl with life science background and this is my first time coding). 
I am reading the book' getting started with processing' and doing some exercises on my own. I have a very silly question to ask you, how do you fill a triangle with diagonal lines? ( i dont want to just set the background to black to cover the lines outside the triangle) 

Thanks

Nicole 

Replies(4)

let's say you have a triangle:
Copy code
  1. triangle(x1,y1, x2,y2, x3,y3);

you can do:

Copy code
  1. int nbLines = 12;//number of lines desired

  2. for(int i=0; i<nbLines; i++){
  3. float xa = map(i,0,nbLines, x1, x3);
  4. float ya = map(i,0,nbLines, y1, y3);
  5. float xb = map(i,0,nbLines, x2, x3);
  6. float yb = map(i,0,nbLines, y2, y3);
  7. line(xa,ya, xb,yb);
  8. }
full example:
Copy code
  1. float x1,y1, x2,y2, x3,y3, nbLines;

  2. void setup(){
  3. size(800, 600, P2D);
  4. smooth();
  5. randomize();
  6. }

  7. void draw(){
  8. background(255);
  9. triangle(x1,y1, x2,y2, x3,y3);
  10. for(int i=0; i<nbLines; i++){
  11. float xa = map(i,0,nbLines, x1, x3);
  12. float ya = map(i,0,nbLines, y1, y3);
  13. float xb = map(i,0,nbLines, x2, x3);
  14. float yb = map(i,0,nbLines, y2, y3);
  15. line(xa,ya, xb,yb);
  16. }

  17. noLoop();
  18. }

  19. void mousePressed(){
  20. randomize();
  21. loop();
  22. }

  23. void randomize(){
  24. x1= random(width);
  25. y1=random(height);
  26. x2= random(width);
  27. y2=random(height);
  28. x3= random(width);
  29. y3=random(height);
  30. nbLines = random(15);
  31. }

Makio135
http://makio135.com/
An alternative option is to draw the lines to a PGraphics and use that as a texture on a polygon (in this case a triangle). Also see this thread.

Code Example (compatible with Processing 1.5.1 & 2.0.1)
Copy code
  1. import processing.opengl.*;
  2.  
  3. int numLines = 100;
  4. float angle = 45;
  5. PGraphics pg;
  6.  
  7. void setup() {
  8.   size(500, 500, OPENGL);
  9.   pg = createGraphics(width, height, JAVA2D);
  10.   drawLines(pg, angle, numLines);
  11. }
  12.  
  13. void draw() {
  14.   background(255);
  15.   // drawLines(pg, frameCount%360, numLines); // rotating lines
  16.   drawTriangle(mouseX, mouseY, 450, 300, 200, 450, pg);
  17. }
  18.  
  19. void drawLines(PGraphics pg, float angle, int num) {
  20.   pg.beginDraw();
  21.   pg.smooth();
  22.   pg.background(0, 0);
  23.   pg.translate(pg.width/2, pg.height/2);
  24.   pg.rotate(radians(angle));
  25.   float w = pg.width*1.5/num;
  26.   for (int i=0; i<num; i++) {
  27.     float x = (i-num/2)*w;
  28.     pg.line(x, -pg.height, x, pg.height);
  29.   }
  30.   pg.endDraw();
  31. }
  32.  
  33. void drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, PGraphics tex) {
  34.   beginShape();
  35.   texture(tex);
  36.   vertex(x1, y1, x1, y1);
  37.   vertex(x2, y2, x2, y2);
  38.   vertex(x3, y3, x3, y3);
  39.   endShape(CLOSE);
  40. }