Creating objects of a cartesian plane: What else can I do?

For self-educational purposes, I'm making a namespace cart with classes point, line, shapes, etc. All with their corresponding values/objects, functions to alter them, and operator overloads.

This is what I wrote so far (with all respective function definitions somewhere else):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class point{
  public:

    double X;
    double Y;

    point
    (
       double _x = 0,
       double _y = 0
    )
    : X(_x), Y(_y)
    {}



    point swappedXY ();             /// Returns a point with positions X and Y swapped with each other from the original
    point reflectedX();             /// Returns a point reflected on the X axis from the original
    point reflectedY();             /// Returns a point reflected on the Y axis from the original
    point inverted  ();             /// Returns a point that has been reflected on both axises from the original

    void swap       ();             /// Swaps the X and Y positions of the point with each other
    void reflectX   ();             /// Reflects the point on the X axis
    void reflectY   ();             /// Reflects the point on the Y axis
    void invert     ();             /// Reflects the point on both axises

    void place (double, double);    /// Gives the point entirely new positions
    void moveby(double, double);    /// Moves the point by adding values to its X and Y positions (as if moving it with a vector)



    bool operator <  (const point); /// Checks if a point's x and y positions are both smaller than another point's
    bool operator >  (const point); /// Checks if a point's x and y positions are both greater than another point's
    bool operator == (const point); /// Checks if two points are in the same position
    bool operator >= (const point); /// Checks if a point's x and y positions are both lesser or equal to another's
    bool operator <= (const point); /// Checks if a point's x and y positions are both greater or equal to another's

    void operator += (const point); /// Summing x and y values to a point from another point
    void operator -= (const point);

    point operator + (const point); /// Returns a point whose X and Y positions are the respective sums of another two points' (for example, if by moving it with a vector)
    point operator - (const point); /// Returns a point whose X and Y positions are the respective differences of another two points'

    point operator + ();            /// Returns the point itself
    point operator - ();            /// Returns a point that has been invert()ed on both axises

    double operator >> (const point); /// Returns the area of a rectangle formed by two opposite vertices
    double operator -> (const point); /// Returns the length of a line formed by two points


};


The class above will be a basis for all future classes' functions, for example; line.reflectX() doing a reflection on both of its points, and triangle.moveby(,) moving all three of its points/lines somewhere else. I think I gave myself enough possibilities with all those operations.



First of all; how do I reference the object itself of a class inside its functions? So as to do something like:
1
2
3
4
point cart::point::operator - ()
{
    return (thePoint).inverted();
}

I think it's done using this, but I can't find any specific information for its usage in this situation.



Second, I'd like to know what I could do with this idea. I'd love for someone of the forums to give me some sort of challenge to perform through this concept (in lack of a teacher...). Also, is there something about the prototypes above that could be improved? Using const more, for example?
Last edited on
Like this:
1
2
3
4
point cart::point::operator - ()
{
    return this->inverted();
}


I can't think how the class 'point' that you made can be of much use. Also, you are missing the other most important thing in cartesian plane - vectors.

Basically, for 2D points and vectors you need to make classes that contain their x,y coordinates. The basic functions operating on points are:

Point2D Move(Point2D p, Vector2D v);
Vector2D Diff(Point2D a, Point2D b);
double Distance(Point2D a, Point2D b);

The basic functions operating on vectors are:

double Vector2D::Length() const;
Vector2D VecMul(Vector2D vec, double k);
Vector2D VecDiv(Vector2D vec, double k);
Vector2D VecAdd(Vector2D v1, Vector2D v2);
Vector2D VecSub(Vector2D v1, Vector2D v2);
Vector2D VecNormalize(Vector2D vec);
double DotProduct(Vector2D v1, Vector2D v2);
Last edited on
About the last question, I couldn't think of anything else but to point you to my book
"Programming for Beginners - with C++" on Amazon. It's currently on on sale so you can get it extremely cheaply.

All the things you are asking are answered in the last third of the book. That part deals with graphics, points, vectors, how to implement them and what to do with them, including example programs with interactive computer graphics animations.
Last edited on
Useful and of interest, thanks a lot. A book is in the realm of possibility. About vectors:

Can't a vector in a cartesian plane simply be described by a point in relation to the origin? With that kind of thought, a vector is just a point. Magnitude and direction are then both implicit in the x and y positions of that point, and obtainable. That's why the functions to move a point in my class accept other points as arguments. If vector-only functions are important, I could make a vector class inheriting point, I believe, right?

For the point class not having much use; no, it probably doesn't. It is much more of a learning step; putting things into practice (Much like most things in this beginner subforum).
Last edited on
Usually programmers do it the other way round: they dispense with points and represent them with vectors (where a point is described by a vecor as the point's displacement from origin).

While you can use only points, that would be unusual and you might run into problems with such abstractions.

Like: what does it mean to add two points together? With vectors, such operations are obvious and natural.

Even worse abstraction would be to attempt doing a dot product of two points. And how's about a vector product (in3D)? It just looks like a bad abstraction.

Mathematicians clearly separate vectors and points, and there are very good reasons for that. I would say that for a beginner, and especially for practicing, it would be the best to separate vectors and points. The functions that I previously recommended are standard matematical operations on vectors and points.
Alright, thank you.
Looking at your class:

operators +, - are better implemented as global functions instead of member funcrions( for symmetry of parameters).

Operator == should not really be used on something that is based on type double. You know, double(0.1) is not exactly 0.1 due to binary system, so you can't really compare values of type double for equality.

operators <, >, <=, >= : it doesn't make much sense to compare points (or vectors), really. I can't think of any uses for such operations.

operator -> : this should really be a global function Distance(...)
operator >> : better to use a global function Area(...) instead
Last edited on
About operator ==: Yes, I was thinking the same thing. But it's useful to know if two points are in the same place, so I was thinking I could compare if the difference between two points is small enough to be considered "basically nothing". Is that a thing I should do? Is there a better way?

Operators >, <, >= and <= are leftovers from previous things, really. I could delete them, but they're not causing any harm.

I replaced operator -> with |, as the compiler didn't like that I used -> in a non-void function for some reason. Is it wrong in any way to have both a global function and operators for | and >>? That wouldn't really change anything. Edit: I actually had already made a double linelength(point, point) function and forgot. Renaming it to distance makes more sense.

Do you mean operators +a, -a, or a+b, a-b ?
Last edited on
a+b and a-b should be global

Operator ==: Use a global function bool isNearby(point a, point b, double dist). There is nothing really wrong with global functions (except that your IDE might not be able to do autocomplete, but that is not a good enough reason). You can put global functions in namespaces if you like.

You seem to really like operators. The problem is, there are not that many of them and it might lead to some confusion. I think that it is the best to reserve the use of operators for context where their meaning is clear. It is ok to use +,-,* for vectors, matrices, complex numbers. For calculating area or distance, I would be much more happy and confident to type the function names 'area' and 'distance' every time. And, function notation makes priority of operations explicit, so there is no wory about order of operations.

Also, about 'this': to get the value of the object itself, use the expression:

*this

since 'this' is a pointer to the current object. So, you can return the whole value of a point with:
1
2
point point::GetPoint() const 
{ return *this; }

.. although there is not much use for such function.
Last edited on
.. although there is not much use for such function.

I laughed at this.


I definitely do like operators. I will try to keep it low, though. Thanks a lot.

Does it make sense to have operators >, <, == that compare the length of lines, in a manner such like line1 < line2? In my opinion, it does. Not so much for + and -, since it's not clear what exactly I am adding.
Last edited on
Does it make sense to have operators >, <, == that compare the length of lines, in a manner such like line1 < line2?
< and > are debatable (personally, I would be confused by code that used relational operators like that), but == definitely not. == should ideally return true iff the two objects are, shall we say, algebraically equivalent. It's fair to say that if a == b then f(a) == f(b), right? Yet there are some operations on segments that can transform unequal segments of equal length into segments of unequal length.
Oh, I forgot another thing:

return this->inverted();

is the same as:

return (*this).inverted();

operator '->' is just a shorter way to write the second expression.

The problem with the second expression is that you must not forget the parentheses, because operator '.' has higher priority than operator '*'.
Topic archived. No new replies allowed.