shapes with holes and rotating a rhombus

hello, i am experimenting with vormplus' code that produces shapes with holes in them. however, the program produces an outer contour that exceeds the height of the screen even though i have used rectMode(CENTER) and assigned the value of the radius in the equation to be the height:

float outerRadius;
float innerRadius;

float outerAngle;
float innerAngle;

void setup()
{
    size(450, 320, P2D);
    smooth();

    outerAngle = TWO_PI / 4;
    innerAngle = TWO_PI / 4;

    rectMode(CENTER);
    noStroke();
}

void draw()
{
    background(#3B8686);

    stroke(255);
    noFill();
    rect(width/2, height/2, 100, 100);
    noStroke();

    outerRadius = height;
    innerRadius = 100;

    fill(#0B486B);

    translate(width/2, height/2);

    beginShape();

    for(int i = 0; i < 4; i++) 
    {
        float x = cos(i * outerAngle) * outerRadius;
        float y = sin(i * outerAngle) * outerRadius;
        vertex(x, y); 

        println("x" + i + ": " + x + ", " + "y" + i + ": " + y); 
       //why does this output negative and scientific notation for x and y?
       // shouldn't the points lie within the plane of the set size? 
       //i would expect points {(0,0) (0, height) (height, height) (height, 0)}
    }

    beginContour();
    for(int i = 0; i < 4; i++) 
    {
        float x = cos(i * innerAngle) * innerRadius;
        float y = sin(i * innerAngle) * innerRadius;
        vertex(x, y);
    }
    endContour();
    rotate(HALF_PI/2);
    endShape(CLOSE);
}

Answers

  • edited February 2014

    Because sin() and cos() will return values from -1 to 1, depending on your input. Given your formular sin(i * outerAngle) * outerRadius the output range will spread from -outerRadius to outerRadius (-height to height).

  • edited February 2014

    hi poersh,

    thanks for the reply. i think the question i should have asked is if there is a way to generating the shape i am looking for with rotating the rhombus only. as it stands, i would have to us scale to make it it the frame of the window by using arbitrary values to figure out what the magnification is.

  • edited February 2014 Answer ✓

    No, translate() only alters the internal transformation matrix, it basically transforms a copy of the values it receives from vertex(), not the values (variables) itself. The reason your outer border is out of screen is that the distance between -height and +height is 2 * height = 2 * 320 = 640, while your dominant screen dimension, the width, is only 450. So your border has to be out of screen by 95px per side.

    Try to set outerRadius to height / 2 and you'll seed your outer borders.

  • edited February 2014

    hi again,

    i thought that would be the solution, but while it brings the whole shape within the borders of the window, what i am seeking is to have the entire shape within the border the window where the points of the rectangle (outerradius) are {(0,0) (0, height) (height, height) (height, 0)}. height/2 works for a circle. i thought 3* height/4 worked, but it still leaves a bit of border (35px at outerRadius =600) outside the window. unfortunately, the ratio changes depending on the height.

    i also realized that i forgot that i was rotating the shape to make it into a square instead of a rhombus, so that's making it difficult as well. this also accounts for my earlier questions about the coordinates. i guess i asked the wrong question to begin with. as soon as i figure out the right question to ask, i'll post it.

    thanks for responding.

  • edited February 2014

    hi,

    forgetting about the rotation was an important issue. you're right, without the rotation height/2 is the correct value for the outerRadius.

    thanks for your help.

  • edited February 2014

    this is for anyone who cares...

    even after drawing sketch, the point of what i wanted to do escaped me for a bit. anyway, this seems to work. the code above creates a rhombus, so i used the points of the rhombus to frame the square i needed:

    float outerRadius;
    float innerRadius;
    
    float outerAngle;
    float innerAngle;
    
    void setup()
    {
        size(800, 800, P2D);
        smooth();
    
        outerAngle = TWO_PI / 4;
        innerAngle = TWO_PI / 4;
    
        rectMode(CENTER);
        noStroke();
    
        outerRadius = height/2;
        innerRadius = 100;
    }
    
    void draw()
    {
        background(0);
    
        translate(width/2, height/2);
        fill(90, 100, 46, 150);
    
        beginShape();
    
        for(int i = 0; i < 4; i++) 
        {
            float x = cos(i * outerAngle) * outerRadius; 
            float y = sin(i * outerAngle) * outerRadius;  
    
            switch(i)
            {
               case 1:
                  vertex(x - outerRadius, y);
                  break;
               case 2:
                  vertex(x, y - outerRadius);
                  break;
               case 3:
                  vertex(x + outerRadius, y);
                  break;
               case 0:
                  vertex(x, y + outerRadius);
                  break;
            }
        }
    
        beginContour();
        for(int i = 0; i < 4; i++) 
        {
            float x = cos(i * innerAngle) * innerRadius; 
            float y = sin(i * innerAngle) * innerRadius; 
    
            switch(i)
            {
               case 1:
                  vertex(x - innerRadius, y);
                  break;
               case 2:
                  vertex(x, y - innerRadius);
                  break;
               case 3:
                  vertex(x + innerRadius, y);
                  break;
               case 0:
                  vertex(x, y + innerRadius);
                  break;
            }
        }
        endContour();
    // float scalar = map(mouseX, 0, width, 0.0, 1.0);
    // scale(-scalar); println(scalar);
        endShape(CLOSE);
    }
    
Sign In or Register to comment.