Skip to content

Instantly share code, notes, and snippets.

@AjayKrP
Last active March 13, 2017 12:40
Show Gist options
  • Save AjayKrP/fbb4e36bc2afb1f886dbff3837453bd5 to your computer and use it in GitHub Desktop.
Save AjayKrP/fbb4e36bc2afb1f886dbff3837453bd5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
#include <GL/glut.h>
double X1, Y1, X2, Y2;
const double D = 50;
float round_value(float v)
{
return floor(v + 0.5);
}
void SolidLines(double X1_, double Y1_, double X2_ , double Y2_)
{
double dx=(X2_-X1_);
double dy=(Y2_-Y1_);
double steps;
float xInc,yInc,x=X1_,y=Y1_;
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
glBegin(GL_POINTS);
glVertex2d(x,y);
int k;
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
glVertex2d(round_value(x), round_value(y));
}
glEnd();
glFlush();
}
void DottedLinesLowDensity(double X1_, double Y1_, double X2_ , double Y2_){
double dx=(X2_-X1_);
double dy=(Y2_-Y1_);
double steps;
float xInc,yInc,x=X1_,y=Y1_;
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
glBegin(GL_POINTS);
glVertex2d(x,y);
int k;
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
if(int(x)%5== 0 || int(y) % 5 == 0){
glVertex2d(round_value(x), round_value(y));
}
}
glEnd();
glFlush();
}
void DottedLinesHighDensity(double X1_, double Y1_, double X2_ , double Y2_){
double dx=(X2_-X1_);
double dy=(Y2_-Y1_);
double steps;
float xInc,yInc,x=X1_,y=Y1_;
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
glBegin(GL_POINTS);
glVertex2d(x,y);
int k;
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
if(int(x)%2== 0 || int(y) % 2 == 0){
glVertex2d(round_value(x), round_value(y));
}
}
glEnd();
glFlush();
}
void drawDifferentLines(void){
SolidLines(X1, Y1, X2, Y2);
DottedLinesLowDensity(X1, Y1 + D, X2, Y2+D);
DottedLinesHighDensity(X1, Y1 + 2*D, X2, Y2 + 2*D);
}
void Init()
{
glClearColor(1.0,1.0,1.0,0);
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
int main(int argc, char **argv)
{
std::cout << "Enter Two Points : \n";
std::cout << "Enter first point :";
std::cin >> X1 >> Y1 ;
std::cout << "Enter second point : ";
std::cin >> X2 >> Y2;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("DDA_Line");
Init();
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(drawDifferentLines);
glutMainLoop();
}
#include <iostream>
#include <math.h>
#include <GL/glut.h>
double X1, Y1, len;
float round_value(float v)
{
return floor(v + 0.5);
}
void LineDDA(double X1_, double Y1_, double X2_, double Y2_)
{
double dx=(X2_-X1_);
double dy=(Y2_-Y1_);
double steps;
float xInc,yInc,x=X1_,y=Y1_;
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
glBegin(GL_POINTS);
glVertex2d(x,y);
int k;
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
glVertex2d(round_value(x), round_value(y));
}
glEnd();
glFlush();
}
void drawSquare(){
//For outer square
LineDDA(X1, Y1, X1, Y1 + len);
LineDDA(X1 , Y1+len, X1+len, Y1 + len);
LineDDA(X1 +len, Y1+len, X1+len, Y1);
LineDDA(X1, Y1, X1+len, Y1 );
//For rhombus
LineDDA(X1, Y1 + len/2, X1 + len/2, Y1 + len);
LineDDA(X1+ len/2, Y1 + len, X1 + len, Y1 + len/2);
LineDDA(X1+len, Y1 + len/2, X1 + len/2, Y1 );
LineDDA(X1+len/2, Y1 , X1 , Y1 + len/2);
//For innermost square
LineDDA(X1+ len/4, Y1 + len/4, X1 + (3*len)/4, Y1 + len/4);
LineDDA(X1 + (3*len)/4, Y1 + len/4, X1 + (3*len)/4, Y1 + (3*len)/4);
LineDDA(X1 + (3*len)/4, Y1 + (3*len)/4, X1 + (len)/4, Y1 + (3*len)/4);
LineDDA(X1 + (len)/4, Y1 + (3*len)/4, X1+ len/4, Y1 + len/4);
}
void drawSlopedSquare(){
//glClear(GL_COLOR_BUFFER_BIT);
LineDDA(X1, len/2, len/2, Y1 + len);
}
void Init()
{
glClearColor(1.0,1.0,1.0,1);
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
int main(int argc, char **argv)
{
std::cout << "Enter one co-ordinate of outer most square : \n";
std::cout << "Enter point :";
std::cin >> X1 >> Y1 ;
std::cout << "Enter edge length of outer most square : ";
std::cin >> len;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("SQUARE FORMATION");
Init();
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(drawSquare);
//glutDisplayFunc(drawSlopedSquare);
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment