Search code examples
staticprocessingdrawing

drawing a rhombus/diamond on processing


I am a absolute beginner in Processing-language and I am struggling with a little Task.

my task is to create a drawing in which one rhombus is in each square, but then vice versa a square in the superimposed rhombus.

I looked up on Processing References and found some usefull Functions, but I am struggling with the Rhombus shape.

Any hint would be good, as I still try to learn it myself


Solution

  • it's a simple math problem, you just have to calculate the 4 positions of your shapes depending on the shape you want to put inside, I made here is an example on how to fit a diamond inside a square:

    float x,y; //x,y coordinate of the square
    float w;  //width of the square
    
    void setup() {
    
      size(500,500);
    
     //initializing the coordinates of the square to a random position
      x=random(100,300);
      y=random(100,300);
      w=random(50,300);
    
    }
    
    void draw() {
    
     //draw black background
     background(0);
     
     //setting up the color and stroke of the shapes
     stroke(255);
     strokeWeight(3);
     noFill();
     
     //drawing the rectangle
     rect(x,y,w,w);
     
     //drawing the diamond inside
     //first point is the middle of the upper side
     float x1= (x+(x+w))/2; 
     float y1= y; 
     
     //second point is the middle of the right side
     float x2= x+w; 
     float y2= (y+(y+w))/2; 
     
     //third point is the middle of the lower side
     float x3= x1; 
     float y3= y+w; 
     
     //forth point is the middle of the left side
     float x4= x; 
     float y4= y2; 
     
     //drawing the diamond using those four points
     quad(x1,y1,x2,y2,x3,y3,x4,y4);
    
    }