GSP 125 Week 2 ILab Rectangles

/ lab2: rectangles // <insert your name here> // read main.cpp, and follow the instructions at the bottom of main.cpp #define NOMINMAX // prevent Windows API from conflicting with "min" and "max" #include <stdio.h> // C-style output. printf(char*,…), putchar(int) #include <windows.h> // SetConsoleCursorPosition(HANDLE,COORD) #include <conio.h> // _getch() /** * moves the console cursor to the given x/y coordinate * 0, 0 is the upper-left hand coordinate. Standard consoles are 80×24. * @param x * @param y */ void moveCursor(int x, int y) { COORD c = {x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); } struct Vec2 { short x, y; Vec2() : x(0), y(0) { } Vec2(int x, int y) : x(x), y(y) { } void add(Vec2 v) { x += v.x; y += v.y; } }; class Rect { Vec2 min, max; public: Rect(int minx, int miny, int maxx, int maxy) :min(minx,miny),max(maxx,maxy) {} Rect(){} void draw(const char letter) const { for(int row = min.y; row < max.y; row++) { for(int col = min.x; col < max.x; col++) { if(row >= 0 && col >= 0) { moveCursor(col, row); putchar(letter); } } } } bool isOverlapping(Rect const & r) const { return !( min.x >= r.max.x || max.x <= r.min.x || min.y >= r.max.y || max.y <= r.min.y); }; } void translate(Vec2 const & delta) { min.add(delta); max.add(delta); } int main() { // initialization Rect userRect(7, 5, 10, 9); Rect rect0(10, 2, 14, 4); Rect rect1(1, 6, 5, 15); int userInput; do { // draw rect0.draw(‘0’); rect1.draw(‘1’); moveCursor(0, 0); // re-print instructions printf("move with ‘w’, ‘a’, ‘s’, and ‘d’"); userRect.draw(‘#’); // user input userInput = _getch(); // update Vec2 move; switch(userInput) { case ‘w’: move = Vec2( 0,-1); break; case ‘a’:

Previous answers to this question


This is a preview of an assignment submitted on our website by a student. If you need help with this question or any assignment help, click on the order button below and get started. We guarantee authentic, quality, 100% plagiarism free work or your money back.

order uk best essays Get The Answer
Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *