Guest User

Untitled

a guest
Oct 31st, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // Can't create a new rectangle, error E0079 expected a type specifier
  2. std::cout << "Enter 2 points (x,y) that will be corners. for ex 400 300 5 15" << std::endl;
  3. int x[2];
  4. int y[2];
  5. std::cin >> x[0] >> y[0] >> x[1] >> y[1];
  6. arrayPtr[freeShapeIndex] = new Rectangle(Vec2(x[0], y[0]), Vec2(x[1], y[1]));
  7.  
  8. Rentangle.hpp
  9. // no difference is there #pragma once or #ifndef
  10. #ifndef REC_H
  11. #define REC_H
  12. #include "Shape.hpp"
  13. #include <array>
  14. class Rectangle :public virtual Shape
  15. {
  16. private:
  17. std::array<Vec2, 2>corners;
  18. Vec2 GetCenter();
  19. public:
  20. Rectangle() = default;
  21. Rectangle(Vec2& p1, Vec2& p2, int index);
  22. virtual void Draw(sf::RenderWindow& window);
  23. virtual ~Rectangle() { fillColor = deleteColor; }
  24. };
  25.  
  26. Rectangle::Rectangle(Vec2& p1, Vec2& p2, int index)
  27. {
  28. corners[0] = p1;
  29. corners[1] = p2;
  30.  
  31. shapeIndex = index;
  32. }
  33.  
  34. Vec2 Rectangle::GetCenter()
  35. {
  36. if (corners[0].x > corners[1].x) { std::swap(corners[0].x, corners[1].x); }
  37. if (corners[0].y > corners[1].y) { std::swap(corners[0].y, corners[1].y); }
  38. int x = (corners[1].x - corners[0].x) / 2 + corners[0].x;
  39. int y = (corners[1].y - corners[0].y) / 2 + corners[0].y;
  40. return Vec2(x, y);
  41. }
  42.  
  43. void Rectangle::Draw(sf::RenderWindow& window)
  44. {
  45. sf::RectangleShape rectangle;
  46. rectangle.setFillColor(fillColor);
  47. rectangle.setPosition(static_cast<float>(corners[0].x), static_cast<float>(corners[0].y));
  48. rectangle.setSize(sf::Vector2f(static_cast<float>(corners[1].x - corners[0].x), static_cast<float>(corners[1].y - corners[0].y)));
  49. window.draw(rectangle);
  50. PrintIndex(window, GetCenter());
  51. }
  52. #endif
Advertisement
Add Comment
Please, Sign In to add comment