Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Last active March 11, 2018 09:12
Show Gist options
  • Save SohanChy/4a40cc94b111c5180de6c6a8cba1bc29 to your computer and use it in GitHub Desktop.
Save SohanChy/4a40cc94b111c5180de6c6a8cba1bc29 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265
using namespace std;
int a,b,r;
float roundVal(float d)
{
return floor(d + 0.5);
}
void displayCircle(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3ub(0, 0, 0);
glPointSize(2.0);
int x=0,y=0;
double result,param;
param = 90.0;
while(param>45.00){
double cosVal= cos (param*PI/180);
double sinVal= sin (param*PI/180);
y=roundVal(r*sinVal);
x=roundVal(r*cosVal);
param--;
glBegin(GL_POINTS);
glVertex2i(x+a,y+b);
glVertex2i(y+a,x+b);
glVertex2i(y+a,-x+b);
glVertex2i(x+a,-y+b);
glVertex2i(-x+a,-y+b);
glVertex2i(-y+a,-x+b);
glVertex2i(-y+a,x+b);
glVertex2i(-x+a,y+b);
glEnd();
glFlush ();
}
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3ub(0, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(-400.0, 400.0, -400.0, 400.0);
}
main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 150);
glutCreateWindow ("Circle");
cout<<"Radius?"<<endl;
cin>>r;
std::cout<<"Input values circle a,b:";
std::cin>>a;
std::cin>>b;
myInit();
glutDisplayFunc(displayCircle);
glutMainLoop();
}
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include<GL/gl.h>
#include <GL/glut.h>
#define PI 3.14159265
using namespace std;
double pointX1, pointY1, pointX2, pointY2;
float roundVal(float d)
{
return floor(d + 0.5);
}
void Line()
{
double dx=(pointX2-pointX1);
double dy=(pointY2-pointY1);
double m = dy/dx;
double theta = atan (m) * 180 / PI;
double x,y,xEnd,yEnd;
cout<<theta<<endl;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex2d(x,y);
double c = y - m*x;
if(theta >= 45 && theta <= -45){
if(dy > 0){
x=pointX1;
y=pointY1;
yEnd = pointY2;
}
else {
x=pointX2;
y=pointY2;
yEnd = pointY1;
}
while(y < yEnd)
{
glVertex2d(roundVal(x), roundVal(y));
y++;
x = (y-c)/m;
c = y - m*x;
}
}
else {
if(dx > 0){
x=pointX1;
y=pointY1;
xEnd = pointX2;
}
else {
x=pointX2;
y=pointY2;
xEnd = pointX1;
}
while(x < xEnd)
{
glVertex2d(roundVal(x), roundVal(y));
x++;
y = m*x + c;
c = y - m*x;
}
}
glEnd();
glFlush();
}
void myInit()
{
glClearColor(1,1,1,0);
glColor3f(0.0,0.0,0.0);
gluOrtho2D(0, 600, 0, 600);
}
void inputPoints()
{
cout<<"Enter start points: "<<endl;
cin>>pointX1;
cin>>pointY1;
pointY1 = pointY1+1;
cout<<"Enter end points:"<<endl;
cin>>pointX2;
pointX2 = pointX2+1;
cin>>pointY2;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,30);
glutInitWindowSize(600,600);
glutCreateWindow("Line Algorithms ");
myInit();
while(true)
{
inputPoints();
glutDisplayFunc(Line);
glutMainLoop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment