Add solution to ex03 (without Makefile)
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 17 Oct 2024 15:35:09 +0000 (17:35 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 17 Oct 2024 15:35:09 +0000 (17:35 +0200)
ex03/Fixed.cpp [new file with mode: 0644]
ex03/Fixed.h [new file with mode: 0644]
ex03/Point.cpp [new file with mode: 0644]
ex03/Point.h [new file with mode: 0644]
ex03/bsp.cpp [new file with mode: 0644]

diff --git a/ex03/Fixed.cpp b/ex03/Fixed.cpp
new file mode 100644 (file)
index 0000000..1d6acbe
--- /dev/null
@@ -0,0 +1,183 @@
+#include "Fixed.h"
+#include <cmath>
+#include <iostream>
+
+Fixed::Fixed(): m_num(0)
+{
+       std::cout << "Default constructor called\n";
+}
+
+Fixed::Fixed(const int init): m_num(init << point_pos)
+{
+       std::cout << "Int constructor called\n";
+}
+Fixed::Fixed(const float init): m_num(roundf(init * (1 << point_pos)))
+{
+       std::cout << "Float constructor called\n";
+}
+
+Fixed::Fixed(const Fixed &other)
+{
+       std::cout << "Copy constructor called\n";
+       *this = other;
+}
+
+Fixed::~Fixed()
+{
+       std::cout << "Destructor called\n";
+}
+
+Fixed  &Fixed::operator=(const Fixed &other)
+{
+       std::cout << "Copy assignment operator called\n";
+       if (this == &other)
+               return (*this);
+       m_num = other.getRawBits();
+       return (*this);
+}
+
+std::ostream   &operator<<(std::ostream &ostream, const Fixed &num)
+{
+       ostream << num.toFloat();
+       return (ostream);
+}
+
+int    Fixed::getRawBits() const
+{
+       return (m_num);
+}
+
+void   Fixed::setRawBits(int const raw)
+{
+       m_num = raw;
+}
+
+int    Fixed::toInt() const
+{
+       return (m_num >> point_pos);
+}
+
+float  Fixed::toFloat() const
+{
+       return (static_cast<float>(m_num) / (1 << point_pos));
+}
+
+bool   Fixed::operator==(const Fixed &other) const
+{
+       return (m_num == other.m_num);
+}
+
+bool   Fixed::operator!=(const Fixed &other) const
+{
+       return (!(*this == other));
+}
+
+bool   Fixed::operator>(const Fixed &other) const
+{
+       return (m_num > other.m_num);
+}
+
+bool   Fixed::operator<(const Fixed &other) const
+{
+       return (!(*this >= other));
+}
+
+bool   Fixed::operator>=(const Fixed &other) const
+{
+       return (*this == other || *this > other);
+}
+
+bool   Fixed::operator<=(const Fixed &other) const
+{
+       return (!(*this > other));
+}
+
+Fixed  Fixed::operator+(const Fixed &other) const
+{
+       Fixed   res;
+
+       res.m_num = m_num + other.m_num;
+       return (res);
+}
+
+Fixed  Fixed::operator-(const Fixed &other) const
+{
+       Fixed   res;
+
+       res.m_num = m_num - other.m_num;
+       return (res);
+}
+Fixed  Fixed::operator*(const Fixed &other) const
+{
+       Fixed   res;
+
+       res.m_num = m_num * other.m_num;
+       if (res.m_num & (1 << (point_pos - 1)))
+       {
+               res.m_num >>= point_pos;
+               ++res.m_num;
+               return (res);
+       }
+       res.m_num >>= point_pos;
+       return (res);
+}
+
+Fixed  Fixed::operator/(const Fixed &other) const
+{
+       Fixed   res;
+
+       res.m_num = (m_num << point_pos) / other.m_num;
+       return (res);
+}
+
+Fixed  Fixed::operator++()
+{
+       ++m_num;
+       return (*this);
+}
+
+Fixed  Fixed::operator++(int)
+{
+       Fixed   res = *this;
+
+       ++*this;
+       return (res);
+}
+
+Fixed  Fixed::operator--()
+{
+       --m_num;
+       return (*this);
+}
+
+Fixed  Fixed::operator--(int)
+{
+       Fixed   res = *this;
+
+       --*this;
+       return (res);
+}
+
+Fixed          &Fixed::min(Fixed &a, Fixed &b)
+{
+       return (const_cast<Fixed&>(Fixed::min(static_cast<const Fixed&>(a), b)));
+}
+
+const Fixed    &Fixed::min(const Fixed &a, const Fixed &b)
+{
+       if (a > b)
+               return (b);
+       return (a);
+}
+
+Fixed          &Fixed::max(Fixed &a, Fixed &b)
+{
+       return (const_cast<Fixed&>(Fixed::max(static_cast<const Fixed&>(a), b)));
+}
+
+const Fixed    &Fixed::max(const Fixed &a, const Fixed &b)
+{
+       if (a > b)
+               return (a);
+       return (b);
+}
diff --git a/ex03/Fixed.h b/ex03/Fixed.h
new file mode 100644 (file)
index 0000000..5e3854a
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef FIXED_H
+# define FIXED_H
+
+# include <iostream>
+
+class Fixed
+{
+       private:
+               static const int        point_pos = 8;
+               int                                     m_num;
+
+       public:
+               Fixed();
+               Fixed(const int init);
+               Fixed(const float init);
+               Fixed(const Fixed &other);
+               ~Fixed();
+
+               Fixed   &operator=(const Fixed &other);
+
+               bool    operator==(const Fixed &other) const;
+               bool    operator!=(const Fixed &other) const;
+               bool    operator>=(const Fixed &other) const;
+               bool    operator<=(const Fixed &other) const;
+               bool    operator>(const Fixed &other) const;
+               bool    operator<(const Fixed &other) const;
+
+               Fixed   operator+(const Fixed &other) const;
+               Fixed   operator-(const Fixed &other) const;
+               Fixed   operator*(const Fixed &other) const;
+               Fixed   operator/(const Fixed &other) const;
+
+               Fixed   operator++();
+               Fixed   operator++(int);
+               Fixed   operator--();
+               Fixed   operator--(int);
+
+               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() const;
+               void    setRawBits(int const raw);
+               float   toFloat() const;
+               int             toInt() const;
+};
+
+std::ostream   &operator<<(std::ostream &ostream, const Fixed &num);
+
+#endif // FIXED_H
diff --git a/ex03/Point.cpp b/ex03/Point.cpp
new file mode 100644 (file)
index 0000000..e3e6c8c
--- /dev/null
@@ -0,0 +1,45 @@
+#include "Point.h"
+#include "Fixed.h"
+
+Point::Point(): x(0), y(0)
+{
+}
+
+Point::Point(Fixed new_x, Fixed new_y): x(new_x), y(new_y)
+{
+}
+
+Point::Point(const Point &other)
+{
+       *this = other;
+}
+
+Point::~Point()
+{
+}
+
+Point  &Point::operator=(const Point &other)
+{
+       if (this == &other)
+               return (*this);
+       x = other.x;
+       y = other.y;
+       return (*this);
+}
+
+//     This function returns either 1 or -1 (or 0) depending on what half-plane
+//     created by splitting the plane by a line a-b the tested point is.
+//     The important thing is that 2 points on the same half-plane will produce the
+//     same sign.
+int    Point::halfplane_sgn(Point a, Point b) const
+{
+       Fixed   pseudo_distance;
+
+       pseudo_distance = (b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x);
+       if (pseudo_distance > 0)
+               return (1);
+       else if (pseudo_distance < 0)
+               return (-1);
+       else
+               return (0);
+}
diff --git a/ex03/Point.h b/ex03/Point.h
new file mode 100644 (file)
index 0000000..b6e9217
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef POINT_H
+# define POINT_H
+
+# include "Fixed.h"
+
+class Point
+{
+       private:
+               Fixed   x;
+               Fixed   y;
+
+       public:
+               Point();
+               Point(Fixed x, Fixed y);
+               Point(const Point &other);
+               ~Point();
+
+               Point   &operator=(const Point &other);
+
+               int     halfplane_sgn(Point a, Point b) const;
+};
+
+bool   bsp(const Point a, const Point b, const Point c, const Point point);
+
+#endif // POINT_H
diff --git a/ex03/bsp.cpp b/ex03/bsp.cpp
new file mode 100644 (file)
index 0000000..4dcc5fb
--- /dev/null
@@ -0,0 +1,8 @@
+#include "Point.h"
+
+bool   bsp(const Point a, const Point b, const Point c, const Point point)
+{
+       return ((point.halfplane_sgn(a, b) == c.halfplane_sgn(a, b))
+                       && (point.halfplane_sgn(a, c) == b.halfplane_sgn(a, c))
+                       && (point.halfplane_sgn(b, c) == a.halfplane_sgn(b, c)));
+}