Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created September 14, 2011 12:45
Show Gist options
  • Save grodtron/1216460 to your computer and use it in GitHub Desktop.
Save grodtron/1216460 to your computer and use it in GitHub Desktop.
drawing with cpp
/*
* COEN 243 Assignment 1, Question 1.
*
* You are working in the IT department a logistics company that transports boxes of cornflakes
* from New York to Montreal. Each box contains 15 ounces of cornflakes. You have been asked to
* implement an application that, for a given order in metric tons, calculates the number of
* cornflakes boxes required to fill the order.
*
* Gordon Bailey (9541098)
* Sep 11, 2011
*/
#include <iostream>
#include <math.h>
using namespace std;
int main(){
// define conversion factors & declare variables
const float BOXES_PER_OUNCE = 1.0 / 15.0;
const float OUNCES_PER_TONNE = 35273.9619;
float weightInTonnes;
int numBoxes;
// get order weight from user
cout << "Enter order weight in metric tons: ";
cin >> weightInTonnes;
// convert tonnes to boxes. Use floor to round down.
numBoxes = floor(weightInTonnes * OUNCES_PER_TONNE * BOXES_PER_OUNCE);
// output result to user
cout << "Number of boxes in order is " << numBoxes << " (Rounded down)\n";
return 0;
}
#include <cstdlib>
#include <math.h>
#include <Magick++.h>
#define PI 3.141592653589793
#define HALF_PI 1.570796326794897
class Spiraler{
private:
int R;
int G;
int B;
int A;
int randR;
int randG;
int randB;
int randA;
public:
Spiraler();
void spiral( int size, int cellSize, int superCellSize, Magick::Image & image);
void drawCell(int x, int y, int cellSize, int superCellSize, Magick::Image & image);
void setColor( int R, int G, int B, int A, int randR, int randG, int randB, int randA );
};
Spiraler::Spiraler(){
setColor(0,0,0,0,0,0,0,0);
}
void Spiraler::spiral( int size, int cellSize, int superCellSize, Magick::Image & image){
using namespace Magick;
bool
keepGoing = true;
int
superCellPixSize = cellSize * superCellSize,
sideLength = size,
sidePos = 0,
xInc = 0,
yInc = 1,
x = 0,
y = - 2 * superCellPixSize,
turnCount = 1;
float theta = HALF_PI;
// Draw it!
while(keepGoing){
// increment the position
y += yInc * superCellPixSize;
x += xInc * superCellPixSize;
sidePos++ ;
drawCell(x, y, cellSize, superCellSize, image);
if( sidePos > sideLength ){
theta -= HALF_PI;
xInc = static_cast<int>( round(cos(theta)) );
yInc = static_cast<int>( round(sin(theta)) );
sidePos = 0;
turnCount++;
if(turnCount > 1){
turnCount = 0;
sideLength -=2;
if(sideLength < 2){
keepGoing = false;
}
}
}
}
}
void Spiraler::drawCell(int x, int y, int cellSize, int superCellSize, Magick::Image & image){
using namespace Magick;
for (int i = 0; i < superCellSize; ++i){
for (int j = 0; j < superCellSize; ++j){
image.fillColor(Color(
(this->randR > 0 ? (std::rand() % this->randR) : 0) + this->R, // R
(this->randG > 0 ? (std::rand() % this->randG) : 0) + this->G, // G
(this->randB > 0 ? (std::rand() % this->randB) : 0) + this->B, // B
(this->randA > 0 ? (std::rand() % this->randA) : 0) + this->A // A
));
// Draw a cell at the appropriate place
// in the super cell
image.draw(DrawableRectangle(
x + (i * cellSize) + 1,
y + (j * cellSize) + 1,
x + ((i + 1) * cellSize) - 2,
y + ((j + 1) * cellSize) - 2
));
}
}
}
void Spiraler::setColor( int R, int G, int B, int A, int randR, int randG, int randB, int randA ){
this->R = R;
this->G = G;
this->B = B;
this->A = A;
this->randR = randR;
this->randG = randG;
this->randB = randB;
this->randA = randA;
}
int main(){
using namespace Magick;
const int
CELL_SIZE = 16,
SUPER_CELL_SIZE = 1,
SUPER_CELL_PIX_SIZE = CELL_SIZE * SUPER_CELL_SIZE,
WIDTH = 32,
HEIGHT = 32,
PIX_WIDTH = SUPER_CELL_PIX_SIZE * WIDTH,
PIX_HEIGHT = SUPER_CELL_PIX_SIZE * HEIGHT;
// initialize imageMagick
InitializeMagick(NULL);
// create our canvas
Image image( Geometry(PIX_WIDTH, PIX_HEIGHT), Color(0x00, 0x00, 0x00, 0) );
Spiraler spiraler;
// DRAW RED
spiraler.setColor(
0,
220,
0,
0,
128,
35,
128,
255
);
spiraler.spiral( HEIGHT/8, CELL_SIZE*2, SUPER_CELL_SIZE*4, image );
spiraler.spiral( HEIGHT/4, CELL_SIZE*4, SUPER_CELL_SIZE, image );
spiraler.spiral( HEIGHT, CELL_SIZE, SUPER_CELL_SIZE, image );
image.write("test_123.png");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment