Add geometry handling.

This commit is contained in:
jmsgrogan 2022-05-15 14:58:31 +01:00
parent 9c116b1efd
commit c1389218f2
37 changed files with 294 additions and 278 deletions

View file

@ -0,0 +1,13 @@
#pragma once
class Rectangle;
class Grid;
class AbstractGeometricItem
{
public:
Rectangle GetBounds() const = 0;
void Sample(Grid* grid) const = 0;
};

View file

@ -1,7 +1,21 @@
list(APPEND geometry_LIB_INCLUDES
AbstractGeometricItem.h
Circle.h
Circle.cpp
DiscretePoint.h
DiscretePoint.cpp
Grid.h
Grid.cpp
Line.h
Line.cpp
Path.h
Path.cpp
Point.h
Point.cpp
Rectangle.cpp)
Rectangle.h
Rectangle.cpp
Triangle.h
Triangle.cpp)
# add the library

0
src/geometry/Circle.cpp Normal file
View file

0
src/geometry/Circle.h Normal file
View file

View file

@ -4,20 +4,20 @@
class DiscretePoint
{
unsigned mX;
unsigned mY;
unsigned mX;
unsigned mY;
public:
DiscretePoint(unsigned x, unsigned y);
DiscretePoint(unsigned x, unsigned y);
~DiscretePoint();
~DiscretePoint();
std::shared_ptr<DiscretePoint> Create(unsigned x, unsigned y);
std::shared_ptr<DiscretePoint> Create(unsigned x, unsigned y);
unsigned GetX() const;
unsigned GetX() const;
unsigned GetY() const;
unsigned GetY() const;
};
using Pixel = DiscretePoint;

0
src/geometry/Grid.cpp Normal file
View file

56
src/geometry/Grid.h Normal file
View file

@ -0,0 +1,56 @@
#pragma once
#include "Rectangle.h"
#include <vector>
class Grid
{
public:
Grid(const Rectangle& bounds)
: mBounds(bounds)
{
mValues = std::vector<double>(mNumX*mNumY, 0.0);
}
Rectangle GetBounds() const
{
return mBounds;
}
double GetXSpacing() const
{
return mBounds.GetWidth()/double(mNumX);
}
double GetYSpacing() const
{
return mBounds.GetHeight()/double(mNumY);
}
std::vector<double> GetValues() const
{
return mValues;
}
void ResetBounds(const Rectangle& bounds)
{
mBounds = bounds;
mValues = std::vector<double>(mNumX*mNumY, 0.0);
}
void SetValues(const std::vector<std::size_t>& indices, double value)
{
for (auto index : indices)
{
mValues[index] = value;
}
}
private:
Rectangle mBounds;
std::vector<double> mValues;
unsigned mNumX{5};
unsigned mNumY{5};
};

0
src/geometry/Line.cpp Normal file
View file

0
src/geometry/Line.h Normal file
View file

0
src/geometry/Path.cpp Normal file
View file

0
src/geometry/Path.h Normal file
View file

View file

@ -1,19 +1,47 @@
#pragma once
#include <memory>
#include <cmath>
class Point
{
double mX;
double mY;
public:
Point(double x, double y);
Point(double x, double y);
~Point();
~Point();
std::shared_ptr<Point> Create(double x, double y);
std::shared_ptr<Point> Create(double x, double y);
double GetX() const
{
return mX;
}
double GetY() const
{
return mY;
}
double GetDistance(const Point& point) const
{
return std::sqrt(mX*point.GetX() + mY*point.GetY());
}
double GetDeltaX(const Point& point) const
{
return point.GetX() - mX;
}
double GetDeltaY(const Point& point) const
{
return point.GetY() - mY;
}
private:
double mX;
double mY;
};
using PointPtr = std::shared_ptr<Point>;

View file

@ -1,7 +1,40 @@
#pragma once
#include "AbstractGeometricItem.h"
#include "Point.h"
class Rectangle
class Rectangle : public AbstractGeometricItem
{
public:
Rectangle(const Point& bottomLeft, const Point& topRight)
: mBottomLeft(bottomLeft),
mTopRight(topRight)
{
}
Rectangle GetBounds() const override
{
return Rectangle(mBottomLeft, mTopRight);
}
void Sample(Grid* grid) const override
{
}
double GetHeight() const
{
return mBottomLeft.GetDeltaY(mTopRight);
}
double GetWidth() const
{
return mBottomLeft.GetDeltaX(mTopRight);
}
private:
Point mBottomLeft;
Point mTopRight;
};

View file

0
src/geometry/Triangle.h Normal file
View file