simplified code for assignment 3 (a and b), added explanation for some stuff and added 3b (earlier only 3a was there)
This commit is contained in:
parent
d46a4ad83f
commit
a71700e42b
@ -1,97 +0,0 @@
|
||||
/*
|
||||
Problem Statement:Write C++ program to draw the following pattern. Use DDA line and Bresenham's
|
||||
circle drawing algorithm. Apply the concept of encapsulation.
|
||||
Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG
|
||||
*/
|
||||
|
||||
// BEGINNING OF CODE
|
||||
#include<iostream>
|
||||
#include<graphics.h>
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
class algo {
|
||||
public:
|
||||
void dda_line(float x1, float y1, float x2, float y2);
|
||||
void bresneham_cir(int r);
|
||||
};
|
||||
|
||||
void algo::dda_line(float x1, float y1, float x2, float y2) {
|
||||
float x,y,dx,dy,step;
|
||||
int i;
|
||||
//step 2
|
||||
dx=abs(x2-x1);
|
||||
dy=abs(y2-y1);
|
||||
cout<<"dy="<<dy<<"\tdx="<<dx;
|
||||
//step 3
|
||||
if(dx>=dy)
|
||||
step=dx;
|
||||
else
|
||||
step=dy;
|
||||
cout<<"\n"<<step<<endl;
|
||||
//step 4
|
||||
float xinc=float((x2-x1)/step);
|
||||
float yinc=float((y2-y1)/step);
|
||||
//step 5
|
||||
x=x1;
|
||||
y=y1;
|
||||
// outtextxy(0,0,"(0,0)");
|
||||
//step 6
|
||||
i=1;
|
||||
while(i<=step) {
|
||||
// cout<<endl<<"\t"<<i<<"\t(x,y)=("<<x<<","<<y<<")";
|
||||
putpixel(320+x,240-y,4);
|
||||
x=x+xinc;
|
||||
y=y+yinc;
|
||||
i=i+1;
|
||||
// delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void algo::bresneham_cir(int r) {
|
||||
float x,y,p;
|
||||
x=0;
|
||||
y=r;
|
||||
p=3-(2*r);
|
||||
while(x<=y) {
|
||||
putpixel(320+x,240+y,1);
|
||||
putpixel(320-x,240+y,2);
|
||||
putpixel(320+x,240-y,3);
|
||||
putpixel(320-x,240-y,5);
|
||||
putpixel(320+y,240+x,6);
|
||||
putpixel(320+y,240-x,7);
|
||||
putpixel(320-y,240+x,8);
|
||||
putpixel(320-y,240-x,9);
|
||||
x=x+1;
|
||||
if(p<0) {
|
||||
p=p+4*(x)+6;
|
||||
}
|
||||
else {
|
||||
p=p+4*(x-y)+10;
|
||||
y=y-1;
|
||||
}
|
||||
// delay(20);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
algo a1;
|
||||
int i;
|
||||
float r,ang,r1;
|
||||
initwindow(630,480);
|
||||
cout<<"Enter radius of circle";
|
||||
cin>>r;
|
||||
a1.bresneham_cir((int)r);
|
||||
ang=3.24/180;
|
||||
float c=r*cos(30*ang);
|
||||
float s=r*sin(30*ang);
|
||||
a1.dda_line(0,r,0-c,0-s);
|
||||
a1.dda_line(0-c,0-s,0+c,0-s);
|
||||
a1.dda_line(0+c,0-s,0,r);
|
||||
r1=s;
|
||||
a1.bresneham_cir((int)r1);
|
||||
getch();
|
||||
closegraph();
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
103
Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm).cpp
Executable file
103
Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm).cpp
Executable file
@ -0,0 +1,103 @@
|
||||
#include <iostream>
|
||||
#include <graphics.h>
|
||||
#include <math.h>
|
||||
using namespace std;
|
||||
|
||||
void bresenhamCircle(int xcord, int ycord, int rad) {
|
||||
// function to create circle using Bresenham algorithm
|
||||
int dec, x, y;
|
||||
dec = 3-2*rad; // dec is the decision parameter
|
||||
x = 0;
|
||||
y = rad; // set y equals to radius
|
||||
|
||||
while (y >= x) {
|
||||
putpixel(xcord+x, ycord+y, 15); // bottom right
|
||||
putpixel(xcord+y, ycord+x, 15); // right side
|
||||
putpixel(xcord+y, ycord-x, 15); // right up
|
||||
putpixel(xcord+x, ycord-y, 15); // top
|
||||
putpixel(xcord-x, ycord-y, 15); // left top
|
||||
putpixel(xcord-y, ycord-x, 15); // left side
|
||||
putpixel(xcord-y, ycord+x, 15); // left bottom
|
||||
putpixel(xcord-x, ycord+y, 15); // bottom
|
||||
x++;
|
||||
// note: 15 is the colour, instead you can also put WHITE, RED, GREEN, etc.
|
||||
|
||||
// decision paramter
|
||||
if (dec > 0 ) {
|
||||
y--;
|
||||
dec = dec+4*(x-y)+10;
|
||||
}
|
||||
else {
|
||||
dec = dec+4*x+6;
|
||||
}
|
||||
// delay(10); // optional delay to watch the circle being made
|
||||
}
|
||||
}
|
||||
|
||||
void ddaLine(float x1, float y1, float x2, float y2) {
|
||||
// DDA line drawing algorithm (will be used for triangle)
|
||||
float dx, dy, steps, xinc, yinc, x, y;
|
||||
|
||||
dx = abs(x2-x1);
|
||||
dy = abs(y2-y1);
|
||||
|
||||
if (dx>dy) {
|
||||
steps = dx;
|
||||
}
|
||||
else {
|
||||
steps = dy;
|
||||
}
|
||||
|
||||
xinc = (x2-x1)/steps;
|
||||
yinc = (y2-y1)/steps;
|
||||
|
||||
x = x1;
|
||||
y = y1;
|
||||
putpixel(round(x), round(y), 15);
|
||||
|
||||
for (int i=0; i<steps; i++) {
|
||||
x = x + xinc;
|
||||
y = y + yinc;
|
||||
putpixel(round(x), round(y), 15);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x1, y1, x2, y2, x3; // variables to store line coordinates
|
||||
int x, y, r; // variables to store circle coordinates
|
||||
|
||||
cout<<endl<<"Enter coordinates:";
|
||||
cout<<endl<<"Enter value for X1:\t";
|
||||
cin>>x1;
|
||||
cout<<endl<<"Enter value for Y1:\t";
|
||||
cin>>y1;
|
||||
cout<<endl<<"Enter value for X2:\t";
|
||||
cin>>x2;
|
||||
|
||||
// calculating values for x3 and y2 for triangle
|
||||
x3 = (x2+x1)/2;
|
||||
y2 = y1 - sqrt(pow((x2-x1), 2) - pow((x2-x1)/2, 2));
|
||||
|
||||
// initializing graphics window
|
||||
int gd=DETECT, gm;
|
||||
initgraph(&gd, &gm, NULL);
|
||||
|
||||
// make triangle
|
||||
ddaLine(x1, y1, x2, y1);
|
||||
ddaLine(x2, y1, x3, y2);
|
||||
ddaLine(x3, y2, x1, y1);
|
||||
|
||||
// make inner circle
|
||||
x = x3;
|
||||
y = y2+2*(y1-y2)/3;
|
||||
r = (y1-y2)/3;
|
||||
bresenhamCircle(x,y,r);
|
||||
|
||||
// make outer circle
|
||||
r = 2*(y1-y2)/3; // instead you can also use "r=2*r", it's the same
|
||||
bresenhamCircle(x,y,r);
|
||||
|
||||
delay(50000);
|
||||
closegraph();
|
||||
return 0;
|
||||
}
|
111
Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm).cpp
Executable file
111
Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm).cpp
Executable file
@ -0,0 +1,111 @@
|
||||
#include <iostream>
|
||||
#include <graphics.h>
|
||||
#include <math.h>
|
||||
using namespace std;
|
||||
|
||||
void bresenhamCircle(int xcord, int ycord, int rad) {
|
||||
// function to draw circle using Bresenhamn algorithm
|
||||
int dec, x, y;
|
||||
dec = 3-2*rad; // dec is decision parameter
|
||||
x = 0;
|
||||
y = rad;
|
||||
|
||||
while (y >= x) {
|
||||
putpixel(xcord+x, ycord+y, 15); // bottom right
|
||||
putpixel(xcord+y, ycord+x, 15); // right side
|
||||
putpixel(xcord+y, ycord-x, 15); // right up
|
||||
putpixel(xcord+x, ycord-y, 15); // top
|
||||
putpixel(xcord-x, ycord-y, 15); // left top
|
||||
putpixel(xcord-y, ycord-x, 15); // left side
|
||||
putpixel(xcord-y, ycord+x, 15); // left bottom
|
||||
putpixel(xcord-x, ycord+y, 15); // bottom
|
||||
x++;
|
||||
// note: 15 is the colour, instead you can also put WHITE, RED, GREEN, etc.
|
||||
|
||||
// decision paramter
|
||||
if (dec > 0) {
|
||||
y--;
|
||||
dec = dec+4*(x-y)+10;
|
||||
}
|
||||
else {
|
||||
dec = dec+4*x+6;
|
||||
}
|
||||
// delay(10); // optional delay to watch the circle being made
|
||||
}
|
||||
}
|
||||
|
||||
void ddaLine(float x1, float y1, float x2, float y2) {
|
||||
// DDA line drawing algorithm
|
||||
float dx, dy, steps, x, y, xinc, yinc;
|
||||
|
||||
dx = abs(x2-x1);
|
||||
dy = abs(y2-y1);
|
||||
|
||||
if (dx>dy) {
|
||||
steps = dx;
|
||||
}
|
||||
else {
|
||||
steps = dy;
|
||||
}
|
||||
|
||||
x = x1;
|
||||
y = y1;
|
||||
putpixel(round(x), round(y), 15);
|
||||
|
||||
xinc = (x2-x1)/steps;
|
||||
yinc = (y2-y1)/steps;
|
||||
|
||||
for (int i=0; i<steps; i++) {
|
||||
x = x + xinc;
|
||||
y = y + yinc;
|
||||
putpixel(round(x), round(y), 15);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x1, y1, x2, y2; // variables to store coordinates of rectangle
|
||||
float X, Y; // variables to store coordinates of rhombus
|
||||
int x, y, r; // variables to store coordinates of circle
|
||||
|
||||
cout<<endl<<"Enter coordinates:";
|
||||
cout<<endl<<"Enter value for X1:\t";
|
||||
cin>>x1;
|
||||
cout<<endl<<"Enter value for Y1:\t";
|
||||
cin>>y1;
|
||||
cout<<endl<<"Enter value for X2:\t";
|
||||
cin>>x2;
|
||||
cout<<endl<<"Enter value for Y2:\t";
|
||||
cin>>y2;
|
||||
|
||||
// initializing graphics window
|
||||
int gd=DETECT, gm;
|
||||
initgraph(&gd, &gm, NULL);
|
||||
|
||||
// making rectangle
|
||||
ddaLine(x1, y1, x1, y2); // left line
|
||||
ddaLine(x1, y2, x2, y2); // bottom line
|
||||
ddaLine(x2, y2, x2, y1); // right line
|
||||
ddaLine(x2, y1, x1, y1); // top line
|
||||
|
||||
// making rhombus
|
||||
X=(x2+x1)/2;
|
||||
Y=(y2+y1)/2;
|
||||
|
||||
ddaLine(X, y1, x2, Y); // top to right edge
|
||||
ddaLine(x2, Y, X, y2); // right to bottom edge
|
||||
ddaLine(X, y2, x1, Y); // bottom to left edge
|
||||
ddaLine(x1, Y, X, y1); // left to top edge
|
||||
|
||||
// making circle inside rhombus
|
||||
/*
|
||||
NEVER REALLY FIGURED OUT HOW TO MAKE THIS ONE. IF YOU KNOW, PLEASE CONTRIBUTE.
|
||||
*/
|
||||
x = (x1+x2)/2;
|
||||
y = (y1+y2)/2;
|
||||
r = sqrt(pow((x2 - x1) / 4, 2) + pow((y2 - y1) / 4, 2));
|
||||
bresenhamCircle(x,y,r);
|
||||
|
||||
delay(50000);
|
||||
closegraph();
|
||||
return 0;
|
||||
}
|
17
README.md
17
README.md
@ -13,13 +13,14 @@ This repository contains codes, write-ups, lab manuals, lab notes and question p
|
||||
2. Assignment A-2 (Cohen Sutherland Line Clipping)
|
||||
- [Using DDA](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20A-2%20%28Cohen%20Sutherland%20Line%20Clipping%29%20%28using%20DDA%29.cpp)
|
||||
- [Using built-in functions](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20A-2%20%28Cohen%20Sutherland%20Line%20Clipping%29%20%28using%20built-in%20function%29.cpp)
|
||||
3. [Assignment A-3 (Pattern using DDA and Bresenham Line Drawing)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20A-3%20%28Pattern%20using%20DDA%20and%20Bresenham%20Line%20Drawing%29.cpp)
|
||||
4. [Assignment B-4 (Transformations)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-4%20%28Transformations%29.cpp)
|
||||
5. [Assignment B-5a (Snowflake)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-5a%20%28Snowflake%29.cpp)
|
||||
6. [Assignment B-5b (Hilbert Curve)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-5b%20%28Hilbert%20Curve%29.cpp)
|
||||
7. [Assignment B-5c (Koch Curve)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-5c%20%28Koch%20Curve%29.cpp)
|
||||
8. [Assignment C-6c (Sunrise and Sunset)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20C-6c%20%28Sunrise%20and%20Sunset%29.cpp)
|
||||
9. [Assignment C-7b (Bouncing Ball)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20C-7b%20%28Bouncing%20Ball%29.cpp)
|
||||
3. [Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20A-3a%20%28Pattern%20using%20DDA%20Line%20and%20Bresenham%20Circle%20Algorithm%29.cpp) (Circle inside a triangle which is inside a bigger circle)
|
||||
4. [Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20A-3b20%28Pattern%20using%20DDA%20Line%20and%20Bresenham%20Circle%20Algorithm%29.cpp) (Circle inside a rhombus which is inside a rectangle)
|
||||
5. [Assignment B-4 (Transformations)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-4%20%28Transformations%29.cpp)
|
||||
6. [Assignment B-5a (Snowflake)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-5a%20%28Snowflake%29.cpp)
|
||||
7. [Assignment B-5b (Hilbert Curve)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-5b%20%28Hilbert%20Curve%29.cpp)
|
||||
8. [Assignment B-5c (Koch Curve)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20B-5c%20%28Koch%20Curve%29.cpp)
|
||||
9. [Assignment C-6c (Sunrise and Sunset)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20C-6c%20%28Sunrise%20and%20Sunset%29.cpp)
|
||||
10. [Assignment C-7b (Bouncing Ball)](https://git.kska.io/sppu-se-comp-codes/CG/src/branch/main/Assignment%20C-7b%20%28Bouncing%20Ball%29.cpp)
|
||||
|
||||
#### Digital Differential Analyzer (DDA):
|
||||
> Digital Differential Analyzer (DDA) is a line-drawing algorithm for digital displays, using incremental calculations to plot points between endpoints.
|
||||
@ -67,4 +68,4 @@ Maintained by:
|
||||
- [notkshitij](https://git.kska.io/notkshitij)
|
||||
- [TanmaySpamzzz](https://git.kska.io/TanmaySpamzzz)
|
||||
- [shh_itsourlittlesecret (Afan)](https://git.kska.io/shh_itsourlittlesecret)
|
||||
- [Kalaskar_admin03](https://git.kska.io/Kalaskar_admin03/)
|
||||
- [Kalaskar_admin03](https://git.kska.io/Kalaskar_admin03/)
|
||||
|
Loading…
Reference in New Issue
Block a user