Skip to content

Commit

Permalink
Add vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 5, 2024
1 parent d5065a3 commit 065c624
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
9 changes: 3 additions & 6 deletions include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
#include <memory>

#include "../include/frame_buffer.hpp"
#include "../include/vector.hpp"
#include "../include/vector3d.hpp"

namespace graphics {
class Display {
private:
// Constants for display
SDL_Window *window;
SDL_Texture *texture;
SDL_Surface *surface;
SDL_Event *event;
SDL_Renderer *renderer;
// std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window;
// std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> texture;
// std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> surface;
// std::unique_ptr<SDL_Event> event;
// std::unique_ptr<SDL_Renderer> renderer;
std::unique_ptr<FrameBuffer> frameBuffer;
std::vector<Vector3D> vertices;

Expand Down
52 changes: 52 additions & 0 deletions include/vector2d.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <cmath>
#include <iostream>

namespace graphics {
// Represents a two-dimensional vector
class Vector2D {
public:
// Constructor to initialize memory
Vector2D() : x(0), y(0) {}
Vector2D(double x, double y) : x(x), y(y) {}

// Destructor to free the memory allocated
~Vector2D() = default;

// The x-coordinate of the vector
double x;
// The y-coordinate of the vector
double y;

// Overload the addition operator
Vector2D operator+(const Vector2D& vec) const {
return Vector2D(this->x + vec.x, this->y + vec.y);
}

// Overload the subtraction operator
Vector2D operator-(const Vector2D& vec) const {
return Vector2D(this->x - vec.x, this->y - vec.y);
}

// Overload the multiplication operator (vector * scalar)
Vector2D operator*(double t) const {
return Vector2D(this->x * t, this->y * t);
}

// Get the magnitude of the vector
double magnitude() const {
return sqrt(x * x + y * y);
}

// Get the unit vector of a vector
Vector2D unit_vector() const {
return *this * (1.0 / magnitude());
}
};

// Overload the standard output stream insertion operator
inline std::ostream& operator<<(std::ostream& out, const Vector2D& vec) {
return out << vec.x << ' ' << vec.y;
}

File renamed without changes.
2 changes: 1 addition & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <SDL2/SDL.h>

#include "../include/vector.hpp"
#include "../include/vector3d.hpp"

namespace graphics {
Display::Display() {
Expand Down
6 changes: 4 additions & 2 deletions src/frame_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ FrameBuffer::FrameBuffer(int width, int height)

void FrameBuffer::drawPixel(int x, int y, const Color &color) {
if (x >= 0 && x < width && y >= 0 && y < height) {
buffer[y * width + x] = static_cast<uint32_t>(color);
buffer[(y * width) + x] = static_cast<uint32_t>(color);
}
}

Expand Down Expand Up @@ -70,7 +70,9 @@ void FrameBuffer::drawRectangle(
void FrameBuffer::drawFilledRectangle(
int x, int y, int width, int height, const Color &color) {
for (int i = 0; i < height; i++) {
drawLine(x, y + i, x + width, y + i, color);
for (int j = 0; j < width; j++) {
drawPixel(x + j, y + i, color);
}
}
}

Expand Down

0 comments on commit 065c624

Please sign in to comment.