00001 /** 00002 *\file main.cpp 00003 * 00004 *\brief 00005 * 00006 *\author Xianming Chen 00007 * 00008 */ 00009 00010 #include "Canvas.h" 00011 #include "color.h" 00012 #include "xmath.h" 00013 #include <iostream> 00014 #include <sstream> 00015 #include <iomanip> 00016 00017 00018 using namespace columbia; 00019 00020 void draw_pixel(Canvas& canvas, int x, int y, int scale, RGB const& col) 00021 { 00022 canvas.SolidRectangle(x*scale, y*scale, (x+1)*scale, (y+1)*scale, col); 00023 } 00024 00025 00026 int main() 00027 { 00028 try 00029 { 00030 Canvas canvas(128, 128); 00031 00032 const int frames = 200; 00033 const int radius = 20; 00034 const int x0 = -canvas.hReso/2 + radius; 00035 const int y0 = -canvas.vReso/2 + radius; 00036 int step_x = 5; 00037 int step_y = 7; 00038 00039 int x = x0, y = y0; 00040 00041 for(int i = 0; i<frames; i++) 00042 { 00043 ostringstream os; 00044 os << setfill('0') << setw(4) << i; 00045 canvas.filename = "frame" + os.str() + ".ppm"; 00046 00047 canvas.Clear(Blue); 00048 canvas.SolidCircle(x, y, radius, Magenta); 00049 canvas.WritePPM(); 00050 00051 // update circle's center: (x, y) 00052 x += step_x, y += step_y; 00053 if(x + radius >= canvas.hReso/2) // move out of right boundary 00054 { 00055 x = canvas.hReso / 2 - 1 - 2 * (x+radius - canvas.hReso/2) - radius; // bounce back 00056 step_x = - step_x; // will move left 00057 } 00058 else if(x - radius < -canvas.hReso/2) // move out of left boundary 00059 { 00060 x = -canvas.hReso/2 + 2 * (-canvas.hReso/2 - (x - radius)) + radius; // bounce back 00061 step_x = - step_x; // will move right 00062 } 00063 00064 if(y + radius >= canvas.vReso/2) // move out of upper boundary 00065 { 00066 y = canvas.vReso / 2 -1 - 2 * (y+radius - canvas.vReso/2) - radius; // bounce back 00067 step_y = - step_y; // will move down 00068 } 00069 else if(y - radius < -canvas.vReso/2) // move out of bottom boundary 00070 { 00071 y = -canvas.vReso/2 + 2 * (-canvas.vReso/2 - (y - radius)) + radius; // bounce back 00072 step_y = - step_y; // will move up. 00073 } 00074 } 00075 } 00076 catch(char const* msg) 00077 { 00078 std :: cerr << msg << std :: endl; 00079 } 00080 00081 00082 return 0; 00083 } 00084 00085 00086 00087 // canvas.SolidRectangle(0,0, 200, 200, Red); 00088 // canvas.SolidTriangle(0, 200, White, 200, 200, White, 100, 250, White); 00089 00090 // canvas.SolidTriangle(-50, 100, White, -200, 100, White, -125, 200, White); 00091 // canvas.SolidTriangle(-250, 250, White, -200, 100, White, -125, 200, White); 00092 00093 // // p = 2a sin(theta) 00094 // int x1, y1; 00095 00096 // for(int theta = 0; theta <= 360; theta += 5) 00097 // { 00098 // double ang = theta * pi / 180; 00099 // double rho = 200 * sin(2 * ang); 00100 // int x2 = columbia::round(rho * cos(ang)), y2 = columbia::round(rho * sin(ang)); 00101 // if(theta != 0) 00102 // canvas.ScanLineSegment(x1, y1, White, x2, y2, White); 00103 // x1 = x2, y1 = y2; 00104 // } 00105 00106 00107 // canvas.SolidTriangle(-150, 250, Yellow, -100, 100, Yellow, -125, 200, Yellow); 00108 00109 00110 00111 00112 // canvas.Clear(White); 00113 00114 // canvas.filename = "scanline.ppm"; 00115 00116 // { 00117 // Canvas canvas1616(16,16); 00118 // multimap<int,int> pixels; 00119 // canvas1616.ScanLineSegment(0, 0,White, 16, 16,White, &pixels); 00120 00121 // // show thesse pixeles 00122 // for (multimap<int, int>::iterator it = pixels.begin(); it != pixels.end(); ++it) 00123 // { 00124 // draw_pixel(canvas, it->second, it->first, 16, Red); 00125 // } 00126 // } 00127 00128 00129 // { 00130 // Canvas canvas1616(16,16); 00131 // multimap<int,int> pixels; 00132 // canvas1616.ScanLineSegment(0, 0, White, 6, 12,White, &pixels); 00133 00134 // // show thesse pixeles 00135 // for (multimap<int, int>::iterator it = pixels.begin(); it != pixels.end(); ++it) 00136 // { 00137 // draw_pixel(canvas, it->second, it->first, 16, Blue); 00138 // } 00139 // } 00140 00141 00142 // { 00143 // Canvas canvas1616(16,16); 00144 // multimap<int,int> pixels; 00145 // canvas1616.ScanLineSegment(0, 0, White, 12, 6,White, &pixels); 00146 00147 // // show thesse pixeles 00148 // for (multimap<int, int>::iterator it = pixels.begin(); it != pixels.end(); ++it) 00149 // { 00150 // draw_pixel(canvas, it->second, it->first, 16, Green); 00151 // } 00152 // } 00153 00154 // { 00155 // Canvas canvas1616(16,16); 00156 // multimap<int,int> pixels; 00157 // canvas1616.ScanLineSegment(0, 0, White, 12, -6,White, &pixels); 00158 00159 // // show thesse pixeles 00160 // for (multimap<int, int>::iterator it = pixels.begin(); it != pixels.end(); ++it) 00161 // { 00162 // draw_pixel(canvas, it->second, it->first, 16, Green); 00163 // } 00164 // } 00165 00166 // canvas.ScanLineSegment(0,0,Red, 12*16, -6*16, Red); 00167 // canvas.ScanLineSegment(0,0,Red, 12*16, 6*16, Red); 00168 // canvas.ScanLineSegment(0,0,Red, 6*16, 12*16, Red); 00169 // canvas.ScanLineSegment(0,0,Red, 16*16, 16*16, Green); 00170 00171 // for(int y = - 256; y <= 256; y += 16) 00172 // { 00173 // canvas.ScanLineSegment( - 256, y, Black, 256, y, Black); 00174 // } 00175 // for(int x = -256; x <= 256; x += 16) 00176 // { 00177 // canvas.ScanLineSegment(x, -256, Black, x, 256, Black); 00178 // } 00179 00180 // canvas.SolidCircle(-16*16, -16*16, 5, Black); 00181 // canvas.SolidCircle(16*16, 16*16, 5, Black); 00182 // canvas.SolidCircle(12*16, 6*16, 5, Black); 00183 // canvas.SolidCircle(12*16, -6*16, 5, Black); 00184 // canvas.SolidCircle(6*16, 12*16, 5, Black); 00185 // canvas.SolidCircle(0, 0, 5, Black); 00186
1.3.6