From a71700e42b40386f55ee43006f003d8d47576dc8 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Tue, 5 Dec 2023 16:05:49 +0530 Subject: [PATCH] simplified code for assignment 3 (a and b), added explanation for some stuff and added 3b (earlier only 3a was there) --- ... using DDA and Bresenham Line Drawing).cpp | 97 --------------- ...A Line and Bresenham Circle Algorithm).cpp | 103 ++++++++++++++++ ...A Line and Bresenham Circle Algorithm).cpp | 111 ++++++++++++++++++ README.md | 17 +-- 4 files changed, 223 insertions(+), 105 deletions(-) delete mode 100644 Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp create mode 100755 Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm).cpp create mode 100755 Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm).cpp diff --git a/Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp b/Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp deleted file mode 100644 index 145eec1..0000000 --- a/Assignment A-3 (Pattern using DDA and Bresenham Line Drawing).cpp +++ /dev/null @@ -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 -#include -#include -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) - step=dx; - else - step=dy; - cout<<"\n"<>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 \ No newline at end of file diff --git a/Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm).cpp b/Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm).cpp new file mode 100755 index 0000000..28efd20 --- /dev/null +++ b/Assignment A-3a (Pattern using DDA Line and Bresenham Circle Algorithm).cpp @@ -0,0 +1,103 @@ +#include +#include +#include +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>x1; + cout<>y1; + cout<>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; +} \ No newline at end of file diff --git a/Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm).cpp b/Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm).cpp new file mode 100755 index 0000000..e89fc44 --- /dev/null +++ b/Assignment A-3b (Pattern using DDA Line and Bresenham Circle Algorithm).cpp @@ -0,0 +1,111 @@ +#include +#include +#include +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>x1; + cout<>y1; + cout<>x2; + cout<>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; +} \ No newline at end of file diff --git a/README.md b/README.md index 3529452..2e99847 100644 --- a/README.md +++ b/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/) \ No newline at end of file +- [Kalaskar_admin03](https://git.kska.io/Kalaskar_admin03/)