简体   繁体   中英

What's wrong with these two classes? How to draw a line and move it in C++, using class “line”and class “point”?

I have two classes, line and point. So far, in point.cpp, I have setpoint, drawpoint, erasepoint, and movepoint (with char as an input). Now, in line.cpp, I need to have setline, drawline, and moveline. My problem is: Point only moves once in the direction I choose, and line won't draw at all.

In point.cpp:

void point::setpoint(int a, int b, int col, char c)
{
    X = a;
    Y = b;
    Character = c;
    Color = col;
}

void point::draw()
{
    gotoxy(X, Y);
    color(Color);
    cout << Character;
}

void point::erase()
{
    gotoxy(X, Y);
    cout << " ";
}

void point::move(char dir)
{
    erase();
    dir = getch();
    if(dir == '/')
    {
        X--;
        Y--;
    }
    else if(dir == 'a' || dir == 72)
        Y--; // up
    else if(dir == 'd' || dir == 77)
        X++; // right
    else if(dir == 'i' || dir == 75)
        X--; // left
    else if(dir == 'b' || dir == 80)
        Y++; // down
    draw();
}

void point::movewithtrace(char dir)
{
    dir = getch();
    if(dir=='/')
    {
        X--;
        Y--;
    }
    else if(dir == 'a' || dir == 72)
        Y--; // up
    else if(dir == 'd' || dir == 77)
        X++; // right
    else if(dir == 'i' || dir == 75)
        X--;// left
    else if(dir == 'b' || dir == 80)
        Y++; // down
    draw();
}

What I have in line.cpp so far:

void line::setline(int t, char d, point o)
{
    size = t;
    direction = d;
    origin = o;
}

void line::drawline()
{
    point aux = origin;
    aux.draw();
    for(int c = 1; c < size; c++)
    {
        aux.movewithtrace(direction);
    }
}

void line::eraseline()
{
    line aux = *this;
    aux.origin.SetCharacter(' ');
    aux.drawline();
}

void line::moveline(char dir)
{
    for(int c = 1; c < size; c++)
    {
        eraseline();
        origin.withtrace(dir);
        drawline();
    }
}

What I have in main:

{
    point p1;
    line l1, l2;
    p1.setpoint(8, 6, 3, '@');
    p1.draw();
    char mp = getch();
    l1.setline(6, 'b', p1);
    l1.drawline();
    l1.moverline(mp);
}

The first thing your move and movewithtrace functions do is overwrite the passed-ion dir with the result of getch() . This is probably not what you want. As a result, your drawline function would chew up all your input, assuming getch is a get-character-from-input function.

Your moveline function probably shouldn't have a loop in it; that's covered by eraseline and drawline .

If you need a better answer, you'll need to describe more about the environment you're running in and what all the class members and methods do, as well as the external functions you're calling. I can't even tell if you're drawing pixels or ASCII-art characters.

PS This is why comments in code are helpful: by explaining in English what you're trying to do, both the design and the implementation can be checked. Without documentation, everyone's just guessing, including yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM