This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
csce441pine64backup/L01/src/main.cpp

88 lines
1.8 KiB
C++
Raw Normal View History

2017-02-17 17:32:44 -06:00
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "Image.h"
// This allows you to skip the `std::` in front of C++ standard library
// functions. You can also say `using std::cout` to be more selective.
// You should never do this in a header file.
using namespace std;
int main(int argc, char **argv)
{
if(argc < 10) {
cout << "Usage: L01 filename width height x1 y1 x2 y2 x3 y3" << endl;
return 0;
}
// Output filename
string filename(argv[1]);
// Width of image
int width = atoi(argv[2]);
// Height of image
int height = atoi(argv[3]);
// Initialize coordinate variables to make things easier
float x1 = atoi(argv[4]);
float y1 = atoi(argv[5]);
float x2 = atoi(argv[6]);
float y2 = atoi(argv[7]);
float x3 = atoi(argv[8]);
float y3 = atoi(argv[9]);
// Vertex 1
vertex v1;
v1.x = x1;
v1.y = y1;
v1.z = 0.0;
// Vertex 2
vertex v2;
v2.x = x2;
v2.y = y2;
v2.z = 0.0;
// Vertex 3
vertex v3;
v3.x = x3;
v3.y = y3;
v3.z = 0.0;
// Triangle
Tri t(v1, v2, v3);
// Find xmin, xmax, ymin, ymax
float xmin = x1;
float xmax = x1;
float ymin = y1;
float ymax = y1;
if(x2 < xmin)
xmin = x2;
if(x3 < xmin)
xmin = x3;
if(x2 > xmax)
xmax = x2;
if(x3 > xmax)
xmax = x3;
if(y2 < ymin)
ymin = y2;
if(y3 < ymin)
ymin = y3;
if(y2 > ymax)
ymax = y2;
if(y3 > ymax)
ymax = y3;
// Create the image. We're using a `shared_ptr`, a C++11 feature.
auto image = make_shared<Image>(width, height);
// Draw a rectangle
for(int y = ymin; y < ymax; ++y) {
for(int x = xmin; x < xmax; ++x) {
unsigned char r = 255;
unsigned char g = 0;
unsigned char b = 0;
image->setPixel(x, y, r, g, b);
}
}
// Write image to file
image->writeToFile(filename);
return 0;
}