// Main.cpp
// Basic colors
// December 6, 2012
// Random colors to fill screen
// Sort horizontal and then vertical
// Mark's input
#include <SFML/Graphics.hpp>
#include <time.h>
#include <iostream>
#include <sstream>
int data[2450][3],total[1350], wheel[12][3];
void quickSort(int[], int, int);
void q_sort(int[], int, int, int);
FILE *pFile;
int main()
{
pFile = fopen("//users//warren//programming//projects//basicolor.txt","r");
int prnt, green, red, blue, x, y, z;
int count,row,col;
char buffer[10], string1[10], string2[10], string3[10];
// Initialize random
srand((unsigned int)time(NULL));
// Create main window
sf::RenderWindow App(sf::VideoMode(2500, 1350), "Basic Colors Graphics");
// Clear screen
App.clear(sf::Color(128, 128, 128));
for(y = 0; y < 12; y++)
{
for(x = 0; x < 3; x++)
{
fscanf(pFile,"%i",&wheel[y]
}
}
// Fill screen with randow colors:
for(y = 0; y < 1350; y++)
{
for(x = 0; x < 2500; x++)
{
z = rand() % 12;
red = wheel[z][0];
green = wheel[z][1];
blue = wheel[z][2];
sf::Color choice(red,green,blue);
sf::RectangleShape recShape;
recShape.setSize(sf::Vector2f(1,1));
recShape.setPosition(x, y);
recShape.setFillColor(choice);
App.draw(recShape);
}
App.display();
}
// Screen filled with random colors:
sf::Image Image = App.capture();
for(count = 1; count < 1350; count++)
{
row = count;
// Sort by rows
y = row -1;
z = 0;
for(x = 0; x < 1000; x++)
{
sf::Color color = Image.getPixel(x,y);
data[z][0] = (int)color.r;
data[z][1] = (int)color.g;
data[z][2] = (int)color.b;
sprintf(string1,"%i",(int)color.r);
sprintf(string2,"%i",(int)color.g);
sprintf(string3,"%i",(int)color.b);
strcpy(buffer,string1);
strcat(buffer,string2);
strcat(buffer,string3);
total[z] = atoi(buffer);
z++;
}
quickSort(total, z, 1);
z = 0;
for(x = 0; x < 1000; x++)
{
red = data[z][0];
green = data[z][1];
blue = data[z][2];
sf::Color choice(red,green,blue);
sf::RectangleShape recShape;
recShape.setSize(sf::Vector2f(1,1));
recShape.setPosition(x, y);
recShape.setFillColor(choice);
App.draw(recShape);
z++;
}
App.display();
}
// Start game loop
while (App.isOpen())
{
// Process events
sf::Event Event;
while (App.pollEvent(Event))
{
// Close window : exit
if (Event.type == sf::Event::Closed) App.close();
//Escape key exit
if((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape)) App.close();
}
}
return EXIT_SUCCESS;
}
//End of main
void quickSort(int total[], int size, int row)
{
q_sort(total, 0, size - 1, row);
}
void q_sort(int total[], int left, int right, int row)
{
int pivot, l_hold, r_hold;
int piv0,piv1,piv2;
l_hold = left;
r_hold = right;
pivot = total
;
piv0 = data
[0];
piv1 = data
[1];
piv2 = data
[2];
while (left < right)
{
while ((total
>= pivot) && (left < right)) right--;
if (left != right)
{
total
= total
;
data
[0] = data
[0];
data
[1] = data
[1];
data
[2] = data
[2];
left++;
}
while ((total
<= pivot) && (left < right)) left++;
if (left != right)
{
total
= total
;
data
[0] = data
[0];
data
[1] = data
[1];
data
[2] = data
[2];
right--;
}
}
total
= pivot;
data
[0] = piv0;
data
[1] = piv1;
data
[2] = piv2;
pivot = left;
piv0 = data
[0];
piv1 = data
[1];
piv2 = data
[2];
left = l_hold;
right = r_hold;
if (left < pivot) q_sort(total, left, pivot-1,row);
if (right > pivot) q_sort(total, pivot+1, right,row);
}