It reduces the number of polygon's vertices using actual pixel coordinates.
It's not really useful, but just to test that the resulting polygon is still reasonable. Initial number of points is 1000000, after "compression" it's 523904 (with default canvas size, before you tried to resize it) - so almost half of vertices were removed but you can still see the reasonable shape. If you resize a canvas to a smaller size, the number of vertices after compression can be something like 5000 and even less. It's easy to 'fool' this algorithm though in this particular case (ellipse is a kind of fringe case, you can easily have a sequence of almost unique vertices (at a pixel level).
#include <cassert>
#include <vector>
public:
PolyTest1(unsigned nVertices);
void Reset(unsigned nVertices);
private:
enum {
kNPointsDefault = 10000
};
std::vector<Double_t> fXs;
std::vector<Double_t> fYs;
};
PolyTest1::PolyTest1(
unsigned nVertices) :
TNamed(
"polygon_compression_test1",
"polygon_compression_test1")
{
Reset(nVertices);
}
void PolyTest1::Reset(unsigned nVertices)
{
assert(
gPad !=
nullptr &&
"Reset, gPad is null");
assert(
gRandom !=
nullptr &&
"Reset, gRandom is null");
if (nVertices < kNPointsDefault) {
Warning(
"Reset",
"resetting nVertices parameter to %u",
unsigned(kNPointsDefault));
nVertices = kNPointsDefault;
}
fXs.resize(nVertices);
fYs.resize(nVertices);
Double_t xMin = 0., xMax = 0., yMin = 0., yMax = 0.;
gPad->GetRange(xMin, yMin, xMax, yMax);
assert(xMax - xMin > 0 && yMax - yMin > 0 && "Reset, invalid canvas' ranges");
const Double_t xCentre = xMin + 0.5 * (xMax - xMin);
const Double_t yCentre = yMin + 0.5 * (yMax - yMin);
for (unsigned i = 0; i < nVertices - 1; ++i) {
}
fXs[nVertices - 1] = fXs[0];
fYs[nVertices - 1] = fYs[0];
}
void PolyTest1::Paint(
const Option_t * )
{
assert(
gPad !=
nullptr &&
"Paint, gPad is null");
gPad->PaintFillArea((
Int_t)fXs.size(), &fXs[0], &fYs[0]);
gPad->PaintPolyLine((
Int_t)fXs.size(), &fXs[0], &fYs[0]);
}
void polytest1()
{
PolyTest1 *polygon = new PolyTest1(1000000);
polygon->SetLineColor(
kBlue);
polygon->SetFillColor(
kRed);
polygon->SetLineWidth(1);
polygon->Draw();
}
int Int_t
Signed integer 4 bytes (int).
double Double_t
Double 8 bytes.
const char Option_t
Option string (const char).
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
The TNamed class is the base class for all named ROOT classes.
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
constexpr Double_t TwoPi()