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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// MyRectangle Lab
#include "stdafx.h"
#include "MyRectangle.h"
#include <iostream>
#include <SFML\Graphics.hpp>
using namespace std;
using namespace sf;
int main(){
MyRectangle start;
start.run();
return 0;
}
[/end main.cpp/]
[MyRectangle.h]
#pragma once
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
class MyRectangle{
public:
MyRectangle();
~MyRectangle();
void run();
int getX(float);
int getY(float);
void setValues(float, float);
void tryAgain(float, float);
double area(double fxy[2], double area);
double prim(double fxy[2], double prim);
double area;
double prim;
private:
float x, y;
double fx, fy; //fx && fy == first x and first y
void assignments(float, float, double[]); //To set up fx and fy
void checkRec(float, float);
double fxy[2];
double area_prim[2];
};
[/end MyRectangle.h/]
[MyRectangle.cpp]
/*
****TO DO:*****
1) Make and algerithem that will tell the user if the object is infact a rectangle (wont need to on a 2D structure but will for pt2).
*/
#include "stdafx.h"
#include "MyRectangle.h"
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
MyRectangle::MyRectangle()
{
x = 1;
y = 1;
}
MyRectangle::~MyRectangle()
{
}
void MyRectangle::run() {
float h = 0, w = 0;
char RS; // RS == restart
do{
system("cls");
cout << "Note: input must be bigger than 0 and less than 20." << endl;
cout << "Please input the width: ";
cin >> w;
cout << "Please input the height:";
cin >> h;
system("cls");
setValues(h, w);
checkRec(h, w);
assignments(x, y, fxy);
RectangleShape rectangle(Vector2f(getX(x), getY(y)));
rectangle.setFillColor(Color::Yellow);
rectangle.setSize(Vector2f(getX(x), getY(y)));
RenderWindow window(VideoMode(400, 400), "Rectangle:");
window.clear();
window.draw(rectangle);
window.display();
cout << fx << fy << endl;
cout << "\t\tNote: Your rectangle will be set to a 1:20 scale.\n\n";
cout << "The area of your rectangle is: " << area << "." << endl;
cout << "The perimiter of your rectangle is: " << prim << "." << endl;
system("pause");
system("cls");
cout << "\t\tWould you like to do it again? (Y/N)";
cin >> RS;
} while (RS == 'Y' || RS == 'y');
}
int MyRectangle::getX(float x) {
x = x * 10;
return x;
}
int MyRectangle::getY(float y) {
y = y * 10;
return y;
}
void MyRectangle::setValues(float width, float height){
x = width;
y = height;
}
void MyRectangle::assignments(float x, float y, double fxy[2]){
fxy[0] = x;
fxy[1] = y;
}
void MyRectangle::checkRec(float x, float y) {
if (x < 0 || x > 20)
tryAgain(x, y);
}
void MyRectangle::tryAgain(float x, float y) {
double x_, y_;
cout << "Please re-enter your width:";
cin >> x_;
cout << "Please re-enter your height:";
cin >> y_;
system("cls");
x = x_;
y = y_;
}
double MyRectangle::area(double fxy[2], double area) {
double fx = fxy[0], fy = fxy[1];
area = fx*fy;
return area;
}
double MyRectangle::prim(double fxy[2], double prim) {
double fx = fxy[0], fy = fxy[1];
prim = (fx * 2) + (fy * 2);
return prim;
}
[/end MyRectangle.cpp/]
//End code
|