Answer the question
In order to leave comments, you need to log in
How to draw a circle in c++?
Hello. Need to draw a circle in a simple ubuntu console project. I am using gcc. I googled, to solve such a problem, windows.h or graphics.h are used everywhere, but gcc swears at this. Please tell me how it is possible to solve this problem.
Answer the question
In order to leave comments, you need to log in
Here is an easy way:
#include <GL/glut.h>
#include <math.h>
// compile:
// gcc -Wall -Wextra -Werror -lglut -lGL -lm -std=gnu99 -D_GNU_SOURCE opengl.c -o openglcircle
#define _U_ __attribute__((__unused__))
// exit by ctrl+q or escape
void keyPressed(unsigned char key, _U_ int x, _U_ int y){
int mod = glutGetModifiers(); // window = glutGetWindow()
if(mod == GLUT_ACTIVE_CTRL && key == 'q') exit(0);
else if(key == 27) exit(0);
}
void DrawCircle(float cx, float cy, float r, int num_segments){
float theta = 2. * M_PI / (float)num_segments;
float c, s, t, x = r, y = 0.;
sincosf(theta, &s, &c);
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++){
glVertex2f(x + cx, y + cy);
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
DrawCircle(0.,0.,0.5,100);
glFlush();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("A Simple Circle");
glutDisplayFunc(display);
glutKeyboardFunc(keyPressed);
glutMainLoop();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *drawCircle(int R, int geom){
if(R > 200 || R < 1) return NULL;
int S, i, Y = 2 * R + 2;
if(geom)
S = Y * (R + 1);
else
S = Y * (Y - 1);
char *buf = malloc(S+1);
if(!buf) return NULL;
memset(buf, ' ', S);
buf[S] = 0;
for(i = Y-1; i < S; i+=Y) buf[i] = '\n';
inline void DrawPixel(int x, int y){
if(geom){
if(y%2==0) buf[Y * y/2 + x] = '*';
}else{
buf[Y * y + x] = '*';
}
}
// Bresenham's circle algorithm
int x = R;
int y = 0;
int radiusError = 1-x;
while(x >= y){
DrawPixel(x + R, y + R);
DrawPixel(y + R, x + R);
DrawPixel(-x + R, y + R);
DrawPixel(-y + R, x + R);
DrawPixel(-x + R, -y + R);
DrawPixel(-y + R, -x + R);
DrawPixel(x + R, -y + R);
DrawPixel(y + R, -x + R);
y++;
if (radiusError < 0){
radiusError += 2 * y + 1;
}else{
x--;
radiusError += 2 * (y - x) + 1;
}
}
return buf;
}
int main(int argc, char **argv){
int i, R;
char *buf;
for(i = 1; i < argc; i++){
if(!(buf = drawCircle(R = atoi(argv[i]), 1))){
printf("Wrong parameter %s\n", argv[i]);
continue;
}
printf("\nCircle with R = %d:\n%s\n", R, buf);
free(buf); buf = NULL;
}
return 0;
}
In the console? And in niks does the console support graphics at all?
For the lab at the university, I had to use Code::Blocks under Wine + graphics.h
The algorithm for drawing a circle is something like this:
//Цикл от 0 до 360 градусов
for (int i = 0; i < 360; i++) {
//Перевод из градусов в радианы
double a = i * M_PI / 180;
//Задаём нужный радиус
double r = 1;
//Вычисляем координаты
double x = r * cos(a);
double y = r * sin(a);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question