【问题标题】:Cutting the bottom side of a circle切割圆的底边
【发布时间】:2019-05-10 03:48:36
【问题描述】:
// #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;
}

我添加了 cmets,以便您可以理解我的代码。代码创建了一个大红色圆圈,并切割了圆圈的右下角,但我只想切割底部。我怎样才能做到这一点?非常感谢您的帮助。

像这样:

【问题讨论】:

    标签: c++ visual-studio opengl


    【解决方案1】:

    如果你想用Secant line切割一个圆,那么你必须定义一个起始角和一个结束角,并指定顶点坐标从圆上的点开始角度到结束点角度。

    Full angle 有 360 度(2*PI 弧度)。底部(南)的角度为 -90 度。

    如果你想在圆的底部切割一部分,那么开始和结束的角度可以这样计算:

    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));
    }
    

    【讨论】:

    • 但是我想切圆的底部,我该怎么做呢?
    猜你喜欢
    • 2011-11-07
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多