NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include<GL/glut.h>
#include <GL/freeglut.h>
#include<stdio.h>
#include<math.h>
#include<cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
/* Function that returns -1,0,1 depending on whether x */
/* is <0, =0, >0 respectively */
#define sign(x) ((x>0)?1:((x<0)?-1:0))
void MenuOne();
void MenuTwo();
using namespace std;
int a,b,c,d,e,f;
int xi,yi,xf,yf,rx,ry;
int first=0, radius;
char ch;
bool checker=false;
int argc;
char** argv;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
{
if(size == 0)
{
size = 20; // default cursor size Changing to numbers from 1 to 20, decreases cursor width
}
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = size;
SetConsoleCursorInfo(console,&lpCursor);
}
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}
int round1(double number)
{
return (number>=0) ? (int)(number+0.5):(int)(number-0.5);
}
void setPixel(GLint x,GLint y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
if(checker){
cout<<"<"<<x<<","<<y<<">"<<endl<<"********"<<endl;
}
}
void LineWithDDA(int x0,int y0,int x1,int y1)
{

int dy=y1-y0;
int dx=x1-x0;
int steps,i;
float xinc,yinc,x=x0,y=y0;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=(float)dx/(float)steps;
yinc=(float)dy/(float)steps;
setPixel(round1(x),round1(y));
for(i=0;i<steps;i++)
{
x+=xinc;
y+=yinc;
setPixel(round1(x),round1(y));
}
glutSwapBuffers();
}
void BresenhamLine(int x1, int y1,int x2, int y2)
{
int dx, dy, x, y, d, s1, s2, swap=0, temp;

x = x1;
y = y1;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
s1 = sign(x2-x1);
s2 = sign(y2-y1);

/* Check if dx or dy has a greater range */
/* if dy has a greater range than dx swap dx and dy */
if(dy > dx){temp = dx; dx = dy; dy = temp; swap = 1;}
else{swap=0;}
/* Set the initial decision parameter and the initial point */
d = 2 * dy - dx;
int i;
for(i = 1; i <= dx; i++)
{
setPixel(x,y);

while(d >= 0)
{
if(swap) {
x = x + s1;
}
else
y = y + s2;
d = d - 2* dx;
}
if(swap) {
y = y + s2;
}
else x = x + s1;
d = d + 2 * dy;
}
glFlush();
glutSwapBuffers();

}


void Ellipse(int xCenter, int yCenter, int rx, int ry)
{
//Region 1
float p=ry*ry-rx*rx*ry+rx*rx/4;
int x=0;
int y=ry;
while(2.0*ry*ry*x <= 2.0*rx*rx*y)
{
if(p < 0)
{
x++;
p = p+2*ry*ry*x+ry*ry;
}
else
{
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;
}
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter-x,yCenter-y);
}

//Region 2
p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
while(y > 0)
{
if(p <= 0)
{
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y+rx*rx;
}
else
{
y--;
p = p-2*rx*rx*y+rx*rx;
}
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter-x,yCenter-y);
}
glFlush();
glutSwapBuffers();

}

void midPointCircleAlgo(int xCenter, int yCenter, int radius)
{
int x = 0;
int y = radius;
float decision = 3-2*radius;
while (x <= y)
{ x++;
if (decision < 0)
{
decision += 2*x+1;
}
else
{
y--;
decision += 2*(x-y)+1;
}
setPixel (xCenter + x, yCenter + y);
setPixel (xCenter - x, yCenter + y);
setPixel (xCenter + x, yCenter - y);
setPixel (xCenter - x, yCenter - y);
setPixel (xCenter + y, yCenter + x);
setPixel (xCenter - y, yCenter + x);
setPixel (xCenter + y, yCenter - x);
setPixel (xCenter - y, yCenter - x);
}
glFlush();
glutSwapBuffers();
}

void BresenhamCircle(int ax, int ay, int bx, int by){
int r = sqrt((bx-ax)*(bx-ax)+(by-ay)*(by-ay));
int x = 0, y = r;
int d = 3-2*r;
while(x<=y)
{
setPixel(ax+x,ay+y);
setPixel(ax+x,ay-y);
setPixel(ax-x,ay+y);
setPixel(ax-x,ay-y);
setPixel(ax+y,ay+x);
setPixel(ax+y,ay-x);
setPixel(ax-y,ay+x);
setPixel(ax-y,ay-x);
if(d<0)
{
d = d+4*x+6;
}
else
{
d = d+4*(x-y)+10;
y--;
}
x++;
}
glutSwapBuffers();
glFlush();

}

void SpecialInput(int key, int x, int y){
// true false elave koordinatlar gosterilsin ya yox?
switch(key){
case GLUT_KEY_F1 :
checker=true;
break;
case GLUT_KEY_F2 :
checker=false;
break;
case GLUT_KEY_LEFT: // left of back
glutLeaveMainLoop();
break;
}
glutPostRedisplay();
}

void mouseClick(int button, int state, int x, int y)
{

//glutSpecialFunc(SpecialInput);
if(button==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
switch(first)
{
case 0:
system("cls");
xi = x;
yi = (500-y);
cout<<"initial point: <"<<xi<<","<<yi<<">"<<endl;
first = 1;
break;
case 1:
xf = x;
yf = (500-y);
cout<<"final point: <"<<xf<<","<<xf<<">"<<endl;
if(a==1){

BresenhamLine(xi,yi,xf,yf);
}
else if(a==2){
LineWithDDA (xi,yi,xf,yf);
}
else if (a==3){
BresenhamCircle(xi,yi,xf,yf);
//cout<<"radius is "<<r<<endl;
}
else if (a==4){
int rap = sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi));
midPointCircleAlgo(xi,yi,rap);
}
else if (a==5){
int rap1=sqrt((xf-250)*(xf-250)+(yf-250)*(yf-250));
int rap2=sqrt((xi-250)*(xi-250)+(yi-250)*(yi-250));
Ellipse(250,250,rap1,rap2);
}
first = 0;
break;
}
}
fflush(stdout); // Force output to stdout
}
void Display()
{ glutSpecialFunc(SpecialInput);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
glColor3f((float)rand() / (float)RAND_MAX,(float)rand() / (float)RAND_MAX,(float)rand() / (float)RAND_MAX);
if(a==1){
if(b==1){
BresenhamLine(c,d,e,f);
}
else
if(b==2){
glutMouseFunc(mouseClick); //replace 1/2/3 to use different mouse functions... :)

glutSwapBuffers();
}
}
if(a==2){
if(b==1){
LineWithDDA(c,d,e,f);
}
if(b==2){
glutMouseFunc(mouseClick); //replace 1/2/3 to use different mouse functions... :)

glutSwapBuffers();
}
}
if(a==3){
if(b==1){
BresenhamCircle(c,d,e,f);
}
if(b==2){
glutMouseFunc(mouseClick); //replace 1/2/3 to use different mouse functions... :)

glutSwapBuffers();
}
}
if(a==4){
if(b==1){
midPointCircleAlgo(c,d,radius);
}
if(b==2){
glutMouseFunc(mouseClick); //replace 1/2/3 to use different mouse functions... :)

glutSwapBuffers();
}
}
if(a==5){
if(b==1){
Ellipse(c,d,rx,ry);
}
if(b==2){
glutMouseFunc(mouseClick); //replace 1/2/3 to use different mouse functions... :)

glutSwapBuffers();
}
}
glEnd();
}
int main(int argc,char** argv)
{
do{
setcursor(0,0);
cout<<"************************************"<<endl;
cout<<"1.Bresenham Line"<<endl<<"2.DDA Line"<<endl<<"3.Bresenham Circle"<<endl<<"4.Mid point Circle"<<endl<<"5.Mid point Ellipse"<<endl;
cout<<"************************************"<<endl;
if(kbhit){
ch=getch();
if(ch==49){
a=1;
}else
if(ch==50){
a=2;
}else
if(ch==51){
a=3;
}else
if(ch==52){
a=4;
}else
if(ch==53){
a=5;
}
system("cls");
}
cout<<"Choose type of input:"<<endl;
cout<<"************************************"<<endl;
cout<<"1.Keyboard"<<endl<<"2.Mouse"<<endl;
cout<<"************************************"<<endl;
if(kbhit){
ch=getch();
if(ch==50){
b=2;
}
else
if(a==4){
b=1;
cout<<"enter x1 and y1"<<endl;
cin>>c>>d;
cout<<"enter radius"<<endl;
cin>>radius;
}else
if(a==5){
b=1;
cout<<"enter x1 and y1"<<endl;
cin>>c>>d;
cout<<"enter radius1"<<endl;
cin>>rx;
cout<<"enter radius2"<<endl;
cin>>ry;
}
else{
b=1;
cout<<"enter x1 and y1"<<endl;
cin>>c>>d;
cout<<"enter x2 and y2"<<endl;
cin>>e>>f;
}
system("cls");
}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
glutCreateWindow("Multi-algorithms");
Init();
glutDisplayFunc(Display);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
glutMainLoop();
system("cls");
}while(true);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.