-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathDDA_Algorithm.cpp
More file actions
35 lines (32 loc) · 776 Bytes
/
DDA_Algorithm.cpp
File metadata and controls
35 lines (32 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<bits/stdc++.h>
using namespace std;
int round_(double a) {
int x = a;
if(a-(double)x<0.5) return x;
return x+1;
}
void DDA(int xs, int ys, int xe, int ye) {
int dx=xe-xs;
int dy=ye-ys;
double steps= abs(dx)>abs(dy)?abs(dx):abs(dy);
double xinc=dx/steps;
double yinc=dy/steps;
int ly=max(ye, ys)+3;
int lx=max(xe, xs)+3;
char grid[ly][lx];
for(int i=0;i<ly;i++) for(int j=0;j<lx;j++) grid[i][j]='.';
double x=xs, y=ys;
grid[round_(y)][round_(x)]='*';
for(int i=0;i<steps;i++) {
x=x+xinc;
y=y+yinc;
grid[round_(y)][round_(x)]='*';
}
for(int i=ly-1;i>=0;i--) {
for(int j=0;j<lx;j++) cout<<grid[i][j];
cout<<endl;
}
}
int main() {
DDA(0, 25, 20, 0);
}