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.

enter image description here

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.


  #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)
       // cout << y1 <<"   "<< y2 << endl;
       // Increase y1 and y2 and Return to the beginning
        if (x1 >= X)
           //startint points
            y1 += 10;
            y2 += 10;
            // Reinitialize x1 and x2
            x1 = -4;
            x2 = -4;
        // sweep again
        moveLine(x1 + STEP, y1, x2  + STEP,y2);



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.


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.)

| improve this answer | |
  • \$\begingroup\$ yes I understand @Michael. I'm also formating my code according to Barry's suggestion, which is much better. But in general do u think that there is a problem in the recursion function? \$\endgroup\$ – Hani Gotc Oct 27 '13 at 17:40
  • \$\begingroup\$ I can't see a problem with it. It's possible that you might run into problems with the stack not being large enough if you increased X and Y enough that you ended up making a very large number of recursive calls. If that became a problem, you could replace the recursion with a loop to get around it. \$\endgroup\$ – Michael Shaw Oct 27 '13 at 18:55

I don't know what your code is doing (it is a function without a return value OR side effects), but one way it could be greatly improved is if you use classes to represent the individual pieces:

struct Point {
    int x, y;

struct LineSegment {
    Point left, right;

    LineSegment(const Point& a, const Point& b) {
        // logic here to make sure you assign 'left' and 'right' correctly

That way, your signature becomes:

void moveLine(LineSegment ls);

int main() {
    Point a{5, 0};
    Point b{5, 10};

    moveLine(LineSegment{a, b});

This makes the logic much clearer. Your function takes a LineSegment rather than 4 ints.

| improve this answer | |
  • \$\begingroup\$ Thank you @Barry. Barry What i am trying to do exactly is to move a vertical segment along the x axis (Sweep a plane). When x1 becomes greater than a threshold then I increase y1 and y2, and then i continue sweeping, Till the coordinate y2 along the Y axis reaches a threshold (y2 is the upper part of the segment), when y2 reaches this threshold I stop. \$\endgroup\$ – Hani Gotc Oct 27 '13 at 16:50
  • \$\begingroup\$ the figure illustrates how the segments are moving from to right. It's similar to a scanner, when u scan a paper \$\endgroup\$ – Hani Gotc Oct 27 '13 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.