From 83c053cb0903b024c1ee38b4273c75ca1fab8d33 Mon Sep 17 00:00:00 2001 From: whaffman Date: Thu, 27 Mar 2025 16:15:11 +0100 Subject: [PATCH] check your work later ! --- ex01/src/Fixed.cpp | 4 +- ex01/src/main.cpp | 4 +- ex02/inc/Fixed.hpp | 6 +- ex02/src/Fixed.cpp | 47 +++++++---- ex02/src/main.cpp | 3 +- ex03/Makefile | 6 +- ex03/inc/Fixed.hpp | 63 +++++++++++++++ ex03/inc/Point.hpp | 37 +++++++++ ex03/src/Fixed.cpp | 196 +++++++++++++++++++++++++++++++++++++++++++++ ex03/src/Point.cpp | 65 +++++++++++++++ ex03/src/bsp.cpp | 33 ++++++++ ex03/src/bsp.h | 19 +++++ ex03/src/main.cpp | 30 +++++++ 13 files changed, 489 insertions(+), 24 deletions(-) create mode 100644 ex03/inc/Fixed.hpp create mode 100644 ex03/inc/Point.hpp create mode 100644 ex03/src/Fixed.cpp create mode 100644 ex03/src/Point.cpp create mode 100644 ex03/src/bsp.cpp create mode 100644 ex03/src/bsp.h create mode 100644 ex03/src/main.cpp diff --git a/ex01/src/Fixed.cpp b/ex01/src/Fixed.cpp index 0c71f51..15924d8 100644 --- a/ex01/src/Fixed.cpp +++ b/ex01/src/Fixed.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/25 11:39:39 by whaffman ######## odam.nl */ +/* Updated: 2025/03/27 10:57:00 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ Fixed::Fixed(const Fixed &fixed) Fixed::Fixed(const float value) { std::cout << "Float constructor called" << std::endl; - fixedPointValue = (int)(value * (1 << fractionalBits)); + fixedPointValue = (int)roundf(value * (1 << fractionalBits)); } Fixed::Fixed(const int value) diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index d1c7a94..2eedfc4 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ -/* Updated: 2025/03/25 11:40:20 by whaffman ######## odam.nl */ +/* Updated: 2025/03/27 10:58:38 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int main(void) Fixed const b(10); Fixed const c(42.42f); Fixed const d(b); - a = Fixed(1234.6321f); + a = Fixed(1234.4321f); std::cout << "a is " << a << std::endl; std::cout << "b is " << b << std::endl; diff --git a/ex02/inc/Fixed.hpp b/ex02/inc/Fixed.hpp index 4858fb1..54eb44b 100644 --- a/ex02/inc/Fixed.hpp +++ b/ex02/inc/Fixed.hpp @@ -6,10 +6,14 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ -/* Updated: 2025/03/25 15:23:47 by whaffman ######## odam.nl */ +/* Updated: 2025/03/27 12:27:16 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ +#pragma once + +#define DEBUG 0 + class Fixed { public: Fixed(); diff --git a/ex02/src/Fixed.cpp b/ex02/src/Fixed.cpp index 1a4e775..7e6d7d5 100644 --- a/ex02/src/Fixed.cpp +++ b/ex02/src/Fixed.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/25 15:28:16 by whaffman ######## odam.nl */ +/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,37 +14,45 @@ #include "Fixed.hpp" #include + + Fixed::Fixed() { fixedPointValue = 0; - std::cout << "Default constructor called" << std::endl; + if (DEBUG) + std::cout << "Default constructor called" << std::endl; } Fixed::Fixed(const Fixed &fixed) { - std::cout << "Copy constructor called" << std::endl; + if (DEBUG) + std::cout << "Copy constructor called" << std::endl; *this = fixed; } Fixed::Fixed(const float value) { - std::cout << "Float constructor called" << std::endl; - fixedPointValue = (int)(value * (1 << fractionalBits)); + if (DEBUG) + std::cout << "Float constructor called" << std::endl; + fixedPointValue = (int)roundf(value * (1 << fractionalBits)); } Fixed::Fixed(const int value) { - std::cout << "Int constructor called" << std::endl; + if (DEBUG) + std::cout << "Int constructor called" << std::endl; fixedPointValue = value * (1 << fractionalBits); } Fixed::~Fixed() { - std::cout << "Destructor called" << std::endl; + if (DEBUG) + std::cout << "Destructor called" << std::endl; } Fixed &Fixed::operator=(const Fixed &fixed) { - std::cout << "Copy assignment operator called" << std::endl; + if (DEBUG) + std::cout << "Copy assignment operator called" << std::endl; fixedPointValue = fixed.fixedPointValue; return *this; } @@ -58,25 +66,32 @@ std::ostream &operator<<(std::ostream &out, const Fixed &fixed) Fixed Fixed::operator+(const Fixed &fixed) const { - return (Fixed(toFloat() + fixed.toFloat())); + Fixed result; + result.setRawBits(getRawBits() + fixed.getRawBits()); + return (result); } Fixed Fixed::operator-(const Fixed &fixed) const { - return (Fixed(toFloat() - fixed.toFloat())); + Fixed result; + result.setRawBits(getRawBits() - fixed.getRawBits()); + return (result); } Fixed Fixed::operator*(const Fixed &fixed) const { - return (Fixed(toFloat() * fixed.toFloat())); + Fixed result; + result.setRawBits((int)(((long long)getRawBits() * fixed.getRawBits()) >> fractionalBits)); + return (result); } Fixed Fixed::operator/(const Fixed &fixed) const { - return (Fixed(toFloat() / fixed.toFloat())); + Fixed result; + result.setRawBits((int)(((long long)getRawBits() << fractionalBits) / fixed.getRawBits())); + return (result); } - Fixed &Fixed::operator++() { fixedPointValue++; @@ -133,13 +148,15 @@ bool Fixed::operator!=(const Fixed &fixed) const int Fixed::getRawBits(void) const { - std::cout << "getRawBits member function called" << std::endl; + if (DEBUG) + std::cout << "getRawBits member function called" << std::endl; return fixedPointValue; } void Fixed::setRawBits(int const raw) { - std::cout << "setRawBits member function called" << std::endl; + if (DEBUG) + std::cout << "setRawBits member function called" << std::endl; fixedPointValue = raw; } diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 2240636..d1807b6 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ -/* Updated: 2025/03/25 15:26:00 by whaffman ######## odam.nl */ +/* Updated: 2025/03/27 12:25:28 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ int main(void) std::cout << a++ << std::endl; std::cout << a << std::endl; std::cout << b << std::endl; + std::cout << Fixed(100) / Fixed(8) << std::endl; std::cout << Fixed::max(a, b) << std::endl; return 0; diff --git a/ex03/Makefile b/ex03/Makefile index 78c1e66..a391db7 100644 --- a/ex03/Makefile +++ b/ex03/Makefile @@ -6,11 +6,11 @@ # By: whaffman +#+ # # +#+ # # Created: 2025/03/24 15:14:58 by whaffman #+# #+# # -# Updated: 2025/03/24 15:15:55 by whaffman ######## odam.nl # +# Updated: 2025/03/27 16:10:22 by whaffman ######## odam.nl # # # # **************************************************************************** # -NAME= -SRC= +NAME= BSP +SRC= main.cpp Fixed.cpp Point.cpp bsp.cpp -include ../common.mk diff --git a/ex03/inc/Fixed.hpp b/ex03/inc/Fixed.hpp new file mode 100644 index 0000000..ad45a96 --- /dev/null +++ b/ex03/inc/Fixed.hpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ +/* Updated: 2025/03/27 16:09:40 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +#define DEBUG 0 + +class Fixed { +public: + Fixed(); + Fixed(const int value); + Fixed(const float value); + Fixed(const Fixed &fixed); + ~Fixed(); + + Fixed &operator=(const Fixed &fixed); + Fixed operator+(const Fixed &fixed) const; + Fixed operator-(const Fixed &fixed) const; + Fixed operator*(const Fixed &fixed) const; + Fixed operator/(const Fixed &fixed) const; + + Fixed &operator++(); + Fixed operator++(int); + Fixed &operator--(); + Fixed operator--(int); + + bool operator>(const Fixed &fixed) const; + bool operator<(const Fixed &fixed) const; + bool operator>=(const Fixed &fixed) const; + bool operator<=(const Fixed &fixed) const; + bool operator==(const Fixed &fixed) const; + bool operator!=(const Fixed &fixed) const; + + static Fixed &min(Fixed &a, Fixed &b); + static const Fixed &min(const Fixed &a, const Fixed &b); + static Fixed &max(Fixed &a, Fixed &b); + static const Fixed &max(const Fixed &a, const Fixed &b); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + + +private: + int fixedPointValue; + static const int fractionalBits = 8; +}; + +std::ostream &operator<<(std::ostream &out, const Fixed &fixed); + + diff --git a/ex03/inc/Point.hpp b/ex03/inc/Point.hpp new file mode 100644 index 0000000..3295a8a --- /dev/null +++ b/ex03/inc/Point.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Point.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/27 13:09:47 by whaffman #+# #+# */ +/* Updated: 2025/03/27 16:09:07 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Fixed.hpp" + +class Point { +public: + Point(); + Point(const Point &point); + Point(float x, float y); + Point(int x, int y); + ~Point(); + + Point &operator=(const Point &point); + + Fixed x(void) const; + Fixed y(void) const; + + void x(Fixed x); + void y(Fixed y); + + +private: + Fixed _x; + Fixed _y; +}; \ No newline at end of file diff --git a/ex03/src/Fixed.cpp b/ex03/src/Fixed.cpp new file mode 100644 index 0000000..7e6d7d5 --- /dev/null +++ b/ex03/src/Fixed.cpp @@ -0,0 +1,196 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ +/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" +#include + + + +Fixed::Fixed() +{ + fixedPointValue = 0; + if (DEBUG) + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(const Fixed &fixed) +{ + if (DEBUG) + std::cout << "Copy constructor called" << std::endl; + *this = fixed; +} + +Fixed::Fixed(const float value) +{ + if (DEBUG) + std::cout << "Float constructor called" << std::endl; + fixedPointValue = (int)roundf(value * (1 << fractionalBits)); +} + +Fixed::Fixed(const int value) +{ + if (DEBUG) + std::cout << "Int constructor called" << std::endl; + fixedPointValue = value * (1 << fractionalBits); +} + +Fixed::~Fixed() +{ + if (DEBUG) + std::cout << "Destructor called" << std::endl; +} +Fixed &Fixed::operator=(const Fixed &fixed) +{ + if (DEBUG) + std::cout << "Copy assignment operator called" << std::endl; + fixedPointValue = fixed.fixedPointValue; + return *this; +} + +std::ostream &operator<<(std::ostream &out, const Fixed &fixed) +{ + out << fixed.toFloat(); + return (out); +} + + +Fixed Fixed::operator+(const Fixed &fixed) const +{ + Fixed result; + result.setRawBits(getRawBits() + fixed.getRawBits()); + return (result); +} + +Fixed Fixed::operator-(const Fixed &fixed) const +{ + Fixed result; + result.setRawBits(getRawBits() - fixed.getRawBits()); + return (result); +} + +Fixed Fixed::operator*(const Fixed &fixed) const +{ + Fixed result; + result.setRawBits((int)(((long long)getRawBits() * fixed.getRawBits()) >> fractionalBits)); + return (result); +} + +Fixed Fixed::operator/(const Fixed &fixed) const +{ + Fixed result; + result.setRawBits((int)(((long long)getRawBits() << fractionalBits) / fixed.getRawBits())); + return (result); +} + +Fixed &Fixed::operator++() +{ + fixedPointValue++; + return *this; +} + +Fixed Fixed::operator++(int) +{ + fixedPointValue++; + return Fixed(*this); +} + +Fixed &Fixed::operator--() +{ + fixedPointValue--; + return *this; +} + +Fixed Fixed::operator--(int) +{ + fixedPointValue--; + return Fixed(*this); +} + +bool Fixed::operator>(const Fixed &fixed) const +{ + return (fixedPointValue > fixed.fixedPointValue); +} + +bool Fixed::operator<(const Fixed &fixed) const +{ + return (fixedPointValue < fixed.fixedPointValue); +} + +bool Fixed::operator>=(const Fixed &fixed) const +{ + return (fixedPointValue >= fixed.fixedPointValue); +} + +bool Fixed::operator<=(const Fixed &fixed) const +{ + return (fixedPointValue <= fixed.fixedPointValue); +} + +bool Fixed::operator==(const Fixed &fixed) const +{ + return (fixedPointValue == fixed.fixedPointValue); +} + +bool Fixed::operator!=(const Fixed &fixed) const +{ + return (fixedPointValue != fixed.fixedPointValue); +} + +int Fixed::getRawBits(void) const +{ + if (DEBUG) + std::cout << "getRawBits member function called" << std::endl; + return fixedPointValue; +} + +void Fixed::setRawBits(int const raw) +{ + if (DEBUG) + std::cout << "setRawBits member function called" << std::endl; + fixedPointValue = raw; +} + +float Fixed::toFloat(void) const +{ + return (float)fixedPointValue / (1 << fractionalBits); +} + +int Fixed::toInt(void) const +{ + return (int) std::roundf( (float)fixedPointValue / (1 << fractionalBits)); +} + +Fixed &Fixed::min(Fixed &a, Fixed &b) +{ + if (a < b) + return a; + return b; +} +const Fixed &Fixed::min(const Fixed &a, const Fixed &b) +{ + if (a < b) + return a; + return b; +} +Fixed &Fixed::max(Fixed &a, Fixed &b) +{ + if (a > b) + return a; + return b; +} +const Fixed &Fixed::max(const Fixed &a, const Fixed &b) +{ + if (a > b) + return a; + return b; +} \ No newline at end of file diff --git a/ex03/src/Point.cpp b/ex03/src/Point.cpp new file mode 100644 index 0000000..8bc28ed --- /dev/null +++ b/ex03/src/Point.cpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Point.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/27 15:45:12 by whaffman #+# #+# */ +/* Updated: 2025/03/27 16:01:14 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Point.hpp" +#include "Fixed.hpp" + +Point::Point() +{ +} + +Point::Point(const Point &point) +{ + *this = point; +} + +Point::Point(float x, float y) +{ + _x = Fixed(x); + _y = Fixed(y); +} + +Point::Point(int x, int y) +{ + _x = Fixed(x); + _y = Fixed(y); +} + +Point::~Point() +{ +} + +Point &Point::operator=(const Point &point) +{ + _x = point.x(); + _y = point.y(); + return *this; +} + +Fixed Point::x(void) const +{ + return _x; +} + +Fixed Point::y(void) const +{ + return _y; +} + +void Point::x(Fixed x) +{ + _x = x; +} +void Point::y(Fixed y) +{ + _y = y; +} \ No newline at end of file diff --git a/ex03/src/bsp.cpp b/ex03/src/bsp.cpp new file mode 100644 index 0000000..c8b6b84 --- /dev/null +++ b/ex03/src/bsp.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* bsp.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/27 16:01:44 by whaffman #+# #+# */ +/* Updated: 2025/03/27 16:07:02 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Point.hpp" +#include "Fixed.hpp" +#include "bsp.h" + +Fixed crossproduct(Point a, Point b, Point c) +{ + Fixed x1 = b.x() - a.x(); + Fixed y1 = b.y() - a.y(); + Fixed x2 = c.x() - a.x(); + Fixed y2 = c.y() - a.y(); + return (x1 * y2 - x2 * y1); +} +bool bsp(Point a, Point b, Point c, Point point) +{ + Fixed res1 = crossproduct(a, b, point); + Fixed res2 = crossproduct(b, c, point); + Fixed res3 = crossproduct(c, a, point); + if ((res1 >= 0 && res2 >= 0 && res3 >= 0) || (res1 <= 0 && res2 <= 0 && res3 <= 0)) + return true; + return false; +} \ No newline at end of file diff --git a/ex03/src/bsp.h b/ex03/src/bsp.h new file mode 100644 index 0000000..e2649aa --- /dev/null +++ b/ex03/src/bsp.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* bsp.h :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/27 16:07:27 by whaffman #+# #+# */ +/* Updated: 2025/03/27 16:07:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Point.hpp" +#include "Fixed.hpp" + +Fixed crossproduct(Point a, Point b, Point c); +bool bsp(Point a, Point b, Point c, Point point); diff --git a/ex03/src/main.cpp b/ex03/src/main.cpp new file mode 100644 index 0000000..14ce5c4 --- /dev/null +++ b/ex03/src/main.cpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ +/* Updated: 2025/03/27 16:14:32 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" +#include "Point.hpp" +#include "bsp.h" + +int main(void) +{ + Point a(0, 0); + Point b(0, 4); + Point c(4, 0); + Point point(2.002f,2.0f); + + if (bsp(a, b, c, point)) + std::cout << "Point is inside the triangle" << std::endl; + else + std::cout << "Point is outside the triangle" << std::endl; + return 0; +} \ No newline at end of file