This commit is contained in:
whaffman 2025-04-04 12:37:17 +02:00
parent d85e2a58e7
commit 17d77d99de
12 changed files with 44 additions and 39 deletions

View File

@ -6,13 +6,14 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
/* Updated: 2025/03/28 10:02:16 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:17:35 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
class Fixed { class Fixed
{
public: public:
Fixed(); Fixed();
Fixed(const Fixed &fixed); Fixed(const Fixed &fixed);
@ -23,5 +24,5 @@ public:
private: private:
int fixedPointValue; int fixedPointValue;
static const int fractionalBits = 8; static const int fractionalBits;
}; };

View File

@ -6,13 +6,15 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/24 15:31:12 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:22:32 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <iostream> #include <iostream>
#include "Fixed.hpp" #include "Fixed.hpp"
const int Fixed::fractionalBits = 8;
Fixed::Fixed() Fixed::Fixed()
{ {
fixedPointValue = 0; fixedPointValue = 0;
@ -32,6 +34,8 @@ Fixed::~Fixed()
Fixed &Fixed::operator=(const Fixed &fixed) Fixed &Fixed::operator=(const Fixed &fixed)
{ {
std::cout << "Copy assignment operator called" << std::endl; std::cout << "Copy assignment operator called" << std::endl;
if (this == &fixed)
return *this;
fixedPointValue = fixed.getRawBits(); fixedPointValue = fixed.getRawBits();
return *this; return *this;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
/* Updated: 2025/03/28 10:17:53 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:16:59 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,7 +32,7 @@ public:
private: private:
int fixedPointValue; int fixedPointValue;
static const int fractionalBits = 8; static const int fractionalBits;
}; };
std::ostream &operator<<(std::ostream &out, const Fixed &fixed); std::ostream &operator<<(std::ostream &out, const Fixed &fixed);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/27 10:57:00 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:22:01 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,8 @@
#include "Fixed.hpp" #include "Fixed.hpp"
#include <cmath> #include <cmath>
const int Fixed::fractionalBits = 8;
Fixed::Fixed() Fixed::Fixed()
{ {
fixedPointValue = 0; fixedPointValue = 0;
@ -45,6 +47,8 @@ Fixed::~Fixed()
Fixed &Fixed::operator=(const Fixed &fixed) Fixed &Fixed::operator=(const Fixed &fixed)
{ {
std::cout << "Copy assignment operator called" << std::endl; std::cout << "Copy assignment operator called" << std::endl;
if (this == &fixed)
return *this;
fixedPointValue = fixed.fixedPointValue; fixedPointValue = fixed.fixedPointValue;
return *this; return *this;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
/* Updated: 2025/03/28 10:17:39 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:16:53 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,7 +55,7 @@ public:
private: private:
int fixedPointValue; int fixedPointValue;
static const int fractionalBits = 8; static const int fractionalBits;
}; };
std::ostream &operator<<(std::ostream &out, const Fixed &fixed); std::ostream &operator<<(std::ostream &out, const Fixed &fixed);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:31:08 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include "Fixed.hpp" #include "Fixed.hpp"
#include <cmath> #include <cmath>
const int Fixed::fractionalBits = 8;
Fixed::Fixed() Fixed::Fixed()
{ {
@ -67,28 +67,28 @@ std::ostream &operator<<(std::ostream &out, const Fixed &fixed)
Fixed Fixed::operator+(const Fixed &fixed) const Fixed Fixed::operator+(const Fixed &fixed) const
{ {
Fixed result; Fixed result;
result.setRawBits(getRawBits() + fixed.getRawBits()); result.fixedPointValue = fixedPointValue + fixed.fixedPointValue;;
return (result); return (result);
} }
Fixed Fixed::operator-(const Fixed &fixed) const Fixed Fixed::operator-(const Fixed &fixed) const
{ {
Fixed result; Fixed result;
result.setRawBits(getRawBits() - fixed.getRawBits()); result.fixedPointValue = fixedPointValue - fixed.fixedPointValue;
return (result); return (result);
} }
Fixed Fixed::operator*(const Fixed &fixed) const Fixed Fixed::operator*(const Fixed &fixed) const
{ {
Fixed result; Fixed result;
result.setRawBits((int)(((long long)getRawBits() * fixed.getRawBits()) >> fractionalBits)); result.fixedPointValue = ((int)(((long long)fixedPointValue * fixed.fixedPointValue) >> fractionalBits));
return (result); return (result);
} }
Fixed Fixed::operator/(const Fixed &fixed) const Fixed Fixed::operator/(const Fixed &fixed) const
{ {
Fixed result; Fixed result;
result.setRawBits((int)(((long long)getRawBits() << fractionalBits) / fixed.getRawBits())); result.fixedPointValue = (int)(((long long)fixedPointValue << fractionalBits) / fixed.fixedPointValue);
return (result); return (result);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
/* Updated: 2025/04/04 11:32:06 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:33:42 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,11 +67,11 @@ int main(void)
std::cout << "min(a, c): " << Fixed::min(a, c) << std::endl; std::cout << "min(a, c): " << Fixed::min(a, c) << std::endl;
std::cout << "max(a, c): " << Fixed::max(a, c) << std::endl; std::cout << "max(a, c): " << Fixed::max(a, c) << std::endl;
// Not possible because of const:
std::cout << "c++: " << a << std::endl; // std::cout << "c++: " << c++ << std::endl;
std::cout << "++c: " << ++a << std::endl; // std::cout << "++c: " << ++c << std::endl;
std::cout << "c--: " << a << std::endl; // std::cout << "c--: " << c-- << std::endl;
std::cout << "--c: " << --a << std::endl; // std::cout << "--c: " << --c << std::endl;
return 0; return 0;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:09:40 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:16:48 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,7 +55,7 @@ public:
private: private:
int fixedPointValue; int fixedPointValue;
static const int fractionalBits = 8; static const int fractionalBits;
}; };
std::ostream &operator<<(std::ostream &out, const Fixed &fixed); std::ostream &operator<<(std::ostream &out, const Fixed &fixed);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/27 13:09:47 by whaffman #+# #+# */ /* Created: 2025/03/27 13:09:47 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:09:07 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:13:03 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,8 +18,7 @@ class Point {
public: public:
Point(); Point();
Point(const Point &point); Point(const Point &point);
Point(float x, float y); Point(Fixed x, Fixed y);
Point(int x, int y);
~Point(); ~Point();
Point &operator=(const Point &point); Point &operator=(const Point &point);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:22:55 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include "Fixed.hpp" #include "Fixed.hpp"
#include <cmath> #include <cmath>
const int Fixed::fractionalBits = 8;
Fixed::Fixed() Fixed::Fixed()
{ {
@ -53,6 +53,8 @@ Fixed &Fixed::operator=(const Fixed &fixed)
{ {
if (DEBUG) if (DEBUG)
std::cout << "Copy assignment operator called" << std::endl; std::cout << "Copy assignment operator called" << std::endl;
if (this == &fixed)
return *this;
fixedPointValue = fixed.fixedPointValue; fixedPointValue = fixed.fixedPointValue;
return *this; return *this;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/27 15:45:12 by whaffman #+# #+# */ /* Created: 2025/03/27 15:45:12 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:01:14 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:23:35 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,16 +22,9 @@ Point::Point(const Point &point)
*this = point; *this = point;
} }
Point::Point(float x, float y)
{
_x = Fixed(x);
_y = Fixed(y);
}
Point::Point(int x, int y) Point::Point(Fixed x, Fixed y) : _x(Fixed(x)), _y(Fixed(y))
{ {
_x = Fixed(x);
_y = Fixed(y);
} }
Point::~Point() Point::~Point()
@ -40,6 +33,8 @@ Point::~Point()
Point &Point::operator=(const Point &point) Point &Point::operator=(const Point &point)
{ {
if (this == &point)
return *this;
_x = point.x(); _x = point.x();
_y = point.y(); _y = point.y();
return *this; return *this;

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
/* Updated: 2025/04/04 11:41:46 by whaffman ######## odam.nl */ /* Updated: 2025/04/04 12:35:39 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,7 +43,7 @@ void doTriangleCheck(const Point &a, const Point &b, const Point &c, const Point
int main(void) int main(void)
{ {
doTriangleCheck(Point(0, 0), Point(5, 0), Point(2.5f, 5.0f), Point(2.5f, 2.0f)); doTriangleCheck(Point(0, 0), Point(5, 0), Point(2.5f, 5), Point(2.5f, 2.0f));
doTriangleCheck(Point(-3, -3), Point(3, -3), Point(0, 3), Point(0, 0)); doTriangleCheck(Point(-3, -3), Point(3, -3), Point(0, 3), Point(0, 0));
doTriangleCheck(Point(1, 1), Point(4, 1), Point(2.5f, 4.0f), Point(2.5f, 2.5f)); doTriangleCheck(Point(1, 1), Point(4, 1), Point(2.5f, 4.0f), Point(2.5f, 2.5f));
doTriangleCheck(Point(-2, -2), Point(2, -2), Point(0, 2), Point(-1, -1)); doTriangleCheck(Point(-2, -2), Point(2, -2), Point(0, 2), Point(-1, -1));