Muddy
Muddy

Reputation: 382

Cutting the bottom side of a circle

// #include loads up library files, the order can matter
// generally load glut.h last
#include <stdio.h> // this library is for standard input and output
#include "glut.h"// this library is for glut the OpenGL Utility Toolkit
#include <math.h>

// this is the initialisation function, called once only
void init() {
    glClearColor(0.0, 0.0, 1.0, 0.0); // set what colour you want the background to be
    glMatrixMode(GL_PROJECTION); // set the matrix mode, we will look at this later
                             // set the projection window size in x an y.
    gluOrtho2D(0.0, 500, 0.0, 500.0);
}

// this is the display function it is called when ever you want to draw something
// all drawing should be called form here
void circle() {
    // draw circle
    float theta;
    glClear(GL_COLOR_BUFFER_BIT); // clear the screen using the background colour
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0); // set colour to red
    for (int i = 0; i < 320; i++) {
        theta = i * 3.142 / 180;
        glVertex2f(190 + 50 * cos(theta), 250 + 70 * sin(theta));
    }
    glEnd();
    glFlush(); // force all drawing to finish
}

// this has the effect of repeatedly calling the display function
void display() {
    circle();
}

// as with many programming languages the main() function is the entry point for execution of the program
int main(int argc, char** argv) {
    glutInit(&argc, argv); //perform the GLUT initialization
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // more initialisation
    glutInitWindowSize(800, 600); // set window position
    glutInitWindowPosition(0, 0); // set window size
    glutCreateWindow("Circle"); // create a display with a given caption for the title bar
    init(); // call init function defined above
    glutDisplayFunc(display); // define what function to call to draw
                       // the last function in the program puts the program into infinite loop
    glutMainLoop();
    // this line exits the program
    return 0;
}

I've added comments so you can understand my code. The code creates a big red circle, and cuts the bottom right side of the circle, but I want to the cut only the bottom side. How can I achieve this? I would really appreciate the help.

Like this:

circle

Upvotes: 2

Views: 151

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

If you want to cut a circle by a Secant line, then you have to define an start angle and an end angle and to specify the vertex coordinates form the point on the circle with the start angle to the point with the end angle.

A Full angle has 360 degrees (2*PI radians). The bottom (south) has an angle of -90 degrees.

If you want to cut a part at the bottom of the circle, then the start and the end angle can be calculated like this:

int cutsegment = 45;
int start = -90 + cutsegment / 2;
int end   = 270 - cutsegment / 2;

for (int i = start; i <= end; i++) {
    theta = i * 3.142 / 180;
    glVertex2f(190 + 50 * cos(theta), 250 + 70 * sin(theta));
}

Upvotes: 1

Related Questions