pokitto / PokittoLib

Library for making programs on Pokitto hardware
22 stars 16 forks source link

DrawLine and clipLine defects #31

Closed HomineLudens closed 6 years ago

HomineLudens commented 6 years ago

It seems there are some problems in the Display::clipLine method. This happen on low res mode but it's more evident in Hi-res mode.

Here some test code for convenience


#include "Pokitto.h"
#include "math.h"

Pokitto::Core mygame;

int xs=0;
int ys=0;
int x=0;
int y=0;
int r=10;
double a=0;

int main ()
{
    mygame.begin();

    xs=Pokitto::Display::width/2;
    ys=Pokitto::Display::height/2;

    while (mygame.isRunning())
    {
        if (mygame.update())
        {

            if(Pokitto::Buttons::aBtn())
            {
                if(Pokitto::Buttons::upBtn()) y--;
                if(Pokitto::Buttons::downBtn()) y++;
                if(Pokitto::Buttons::leftBtn()) x--;
                if(Pokitto::Buttons::rightBtn()) x++;
            }
            else
            {
                if(Pokitto::Buttons::upBtn()) ys--;
                if(Pokitto::Buttons::downBtn()) ys++;
                if(Pokitto::Buttons::leftBtn()) xs--;
                if(Pokitto::Buttons::rightBtn()) xs++;
            }

            //Comment from here to adjust points by hand
            a+=0.1;
            r++;
            x=xs+r*sin(a);
            y=ys+r*cos(a);
            //to here.

            Pokitto::Display::drawLine(xs,ys,x,y);

        }
    }

}
Pharap commented 6 years ago

This isn't a bug with clipLine.

The purpose of clipLine is:

/* Clip line with screen boundaries, returns 0 if whole line is out of bounds /

And the line is never completely out of bounds, so clipLine is doing its job properly.

This is however a bug with drawLine. drawLine is attempting to draw pixels offscreen, which it shouldn't be. The solution to this is to limit the end point somehow.

Checking before drawing a pixel would be the easiest solution, but I think that might be a bit slow. So the other solution is to force the limit before entering the loop.

I'm not very good at Bresenham's algorithm so it'll probably take me some time to figure out the best way to handle it, but I'll have a look.

Pharap commented 6 years ago

Disregard my earlier comment. I have proof positive that the problem is with clipLine.

I didn't look properly at what clipLine was trying to do.

HomineLudens commented 6 years ago

Nice catch. Was biting me from a long time.

HomineLudens commented 6 years ago

Nice catch. Was biting me from a long time.

Pharap commented 6 years ago

@Effer For future reference, the PR would have closed this issue automatically when it's merged because I wrote 'Fixes #' and then the issue number.