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

Move Line across Plane

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

Problem

I want move a Vertical line segment across a plane or another way sweep the plane, from Left to right.

The figure illustrates how the segment is moving at the X-axis. When x1 >= X beginning and i translate it to the upper part and so on, till y2 which is the upper part of the segment reaches Y. You can think of it as how a scanner works.

Line = (x1,y1,x2,y2)

When x1 pr x2 coordinates becomes greater or equal to -rightBorder I increase y1 and y2 to the next level and so on till y2 becomes greater than Y.

Algorithm:

#define STEP 9
  #define Y 20
  #define X 30
    void moveLine(int, int, int, int);
    int main()
    {
            moveLine(5, 0, 5, 10);
            return 0;

    }

    void moveLine(int x1, int y1, int x2, int y2)
    {
        //Reaches upper border (Y-axis)
        if (y1 >= Y)
        {
            return;
        }
       // cout = X)
        {
           //startint points
            y1 += 10;
            y2 += 10;
            // Reinitialize x1 and x2
            x1 = -4;
            x2 = -4;
        }
        // sweep again
        moveLine(x1 + STEP, y1, x2  + STEP,y2);

    }


Explanation:

I start with x1 = 5, and x2 = 5, y1 = 5 and y2 = 10 then x1+=9, x2+=9 till x1 >= X, then I reinitialize the x1 and x2, and increase y1 by 5 and y2 + 5 till y2 becomes greater than Y.

I wrote the piece of Code. it worked ok, but I wanted to your advice and if the recursion function is okay.

Thank you.

Solution

Your code has a lot of magic numbers. I can't tell what the significance of the number 20 that is being compared to y1, or the significance of the 20 that is being compared to x1, or whether the two numbers are the same or different (if I changed the number compared to y1 to 30, should I also change the number compared to x1?). One of the comparisons uses > and the other uses >=. I can't tell whether this is what you meant to do or a bug.

If you created named constants with descriptive names, probably all of those things would be obvious.

I think you have a misconception about how function arguments work. Despite having the same name and the same type, the int x1 in main() and the int x1 in moveLine() are different, and the x1 in each recursive call of moveLine() are different.

The line moveLine(x1 += 9, y1, x2+=9,y2); should work, but writing it as moveLine(x1+9, y1, x2+9, y2); is less confusing and would also work.

Also, this code:

int x1 = 5;
int y1 = 0;
int x2 = 5;
int y2 = 10;
moveLine(x1, y1, x2, y2);


could be written like this:

moveLine(5, 0, 5, 10);


(I'm not suggesting you keep the 4 int style; Barry's suggestion is good. I'm just pointing out what looks like a misconception and trying to clear it up.)

Code Snippets

int x1 = 5;
int y1 = 0;
int x2 = 5;
int y2 = 10;
moveLine(x1, y1, x2, y2);
moveLine(5, 0, 5, 10);

Context

StackExchange Code Review Q#33328, answer score: 3

Revisions (0)

No revisions yet.