HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

De Casteljau's algorithm to draw Bezier Curves in OOP and C++ (iterative version)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
iterativeversiondrawalgorithmbeziercurvescasteljauandoop

Problem

#include 
#include "Point2d.h"
#include "Collection.h"
#include "Coordinates2d.h"
#include "graphics.h"    

#define PRECISION 0.01

Point2d Approximate(double t, Point2d const & pt1, Point2d const & pt2)
{
    double x = pt1.x * (1-t) + pt2.x * t;
    double y = pt1.y * (1-t) + pt2.y * t;

    return Point2d(x, y);
}

Collection GetBezierPoints(Collection const & points)
{
    Collection bezierPoints;

    for(double t = 0 ; t  temp1 = points;

        while(temp1.size()>1)
        {
            Collection temp2;

            for(int i = 0 ; i points;

    points.Add(Point2d(0,0));
    points.Add(Point2d(80,300));    
    points.Add(Point2d(120,-150));
    points.Add(Point2d(200,0));

    Collection list = GetBezierPoints(points);

    Coordinates2d::Draw(list);

    Coordinates2d::Wait();

    return 0;
}


NOTE: I am in love with my Collection class, which is a wrapper around std::vector.

Solution

I am repeating much of what is already in the comments but felt it should be spelled out more explicitly.

Avoid using #define statements

Per Scott Meyers's Effective C++ you should always prefer const to #define statements because #define statements are a preprocessor step, which means when you are debugging, you will see a number and not a variable name. This can make your life very difficult, so you should avoid #defines whenever possible.

Descriptive Naming

You should only use a name like temp when it's really important to emphasize that you are using a temporary variable (say to draw your reader/client's attention to the fact that a function creates a temp object and so should be used sparingly when certain memory concerns are present). Quite literally any other name would be better than temp possibly even var. Why not use pointCollection or something similarly descriptive?

Comments where helpful

I don't see any comments in your code. I am also not familiar with the libraries you are using. If you want a reader like me to understand your code, you should put in some clarification, succinct one line statements should be enough. Even for yourself, you should put in a note about what you hoped to accomplish in a function.

Context

StackExchange Code Review Q#100484, answer score: 3

Revisions (0)

No revisions yet.