-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw1.cpp
139 lines (112 loc) · 3.95 KB
/
hw1.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************
* Homework 1: Rasterization *
* CS 148 (Summer 2016), Stanford University *
*-----------------------------------------------------*
* Here you will implement the circle rasterization *
* method you derived in the written portion of the *
* homework. *
* To compile this in linux: *
* g++ hw1.cpp *
* Then, run the program as follows: *
* ./a.out 200 *
* to generate a 200x200 image containing a circular *
* arc. Note that the coordinate system we're using *
* places pixel centers at integer coordinates and *
* has the origin at the lower left. *
* Your code will generate a .ppm file containing the *
* final image. These images can be viewed using *
* "display" in Linux or Irfanview in Mac/Windows. *
*******************************************************/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cassert>
using namespace std;
// We'll store image info as globals; not great programming practice
// but ok for this short program.
// I changed variable to sie to imageSize due to ambiguity errors with Visual Studio code.
int imageSize;
bool **image;
void renderPixel(int x, int y)
{
assert(x >= 0 && y >= 0 && x <= imageSize && y <= imageSize);
// Then we ensure that while x is less than or equal to y (so that the arc's middle pixel maps),
// we render the pixels at (x, y) and its symmetrical pixel at (y, x).
// So, the arc is being rendered from two points: from (0, radius) and from (radius, 0).
if(x <= y){
image[x][y] = 1;
image[y][x] = 1;
}
}
void rasterizeArc(int radius)
{
// We initialize the initial value of x to 0 and y to the radius / imageSize accepted from the argument line.
// We also set our argument, d, to 0 which will be used to determine if we choose to render the next pixel
// East or Southeast of our current pixel.
int x = -1;
int y = radius;
int d = 2 - (2*radius);
// We create a while function for x to be less than the radius, for we are only trying to draw one quadrant
// of an arc.
while(x < radius){
// If d is less than 0, then we render the pixel East of our current pixel:
// we increment d by our East function: 2x + 3,
// increment x by 1, and pass x and y into RenderPixel.
if(d < 0){ // moving East
d += (2*x) + 3;
x += 1;
renderPixel(x, y);
}
// If d is less than 0, then we render the pixel Southeast of our current pixel:
// we increment d by our East function: 2x - 2y +5,
// increment x by 1, decrement y by 1, and pass x and y into RenderPixel.
else if(0 <= d){ // moving SouthEast
d += (2*x) - (2*y) + 5;
x += 1;
y -= 1;
renderPixel(x, y);
}
}
}
// You shouldn't need to change anything below this point.
int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "Usage: " << argv[0] << " circleimageSize\n";
return 0;
}
#ifdef _WIN32
sscanf_s(argv[1], "%d", &imageSize);
#else
sscanf(argv[1], "%d", &imageSize);
#endif
if (imageSize <= 0)
{
cout << "Image must be of positive imageSize.\n";
return 0;
}
// reserve image as 2d array
image = new bool *[imageSize + 1];
for (int i = 0; i <= imageSize; i++)
image[i] = new bool[imageSize + 1];
// pixelTest(imageSize);
rasterizeArc(imageSize);
char filename[50];
#ifdef _WIN32
sprintf_s(filename, 50, "circle%d.ppm", imageSize);
#else
sprintf(filename, "circle%d.ppm", imageSize);
#endif
ofstream outfile(filename);
outfile << "P3\n# " << filename << "\n";
outfile << imageSize + 1 << ' ' << imageSize + 1 << ' ' << 1 << endl;
for (int i = 0; i <= imageSize; i++)
for (int j = 0; j <= imageSize; j++)
outfile << image[imageSize - i][j] << " 0 0\n";
// delete image data
for (int i = 0; i <= imageSize; i++)
delete[] image[i];
delete[] image;
return 0;
}