Question

Lets say we have a 400x800 canvas and I want to draw a line connecting the points P1 (10,10) and P2(500000,800000). As you can see the second point is far outside the canvas boundaries. If I use canvas.darwLine(p1.x, p1.y,p2.x,p2.y,paint) the app freezes and the app becomes unusable. using a clipping doesn't solve the problem, the drawing engine is still trying to draw the pixels to the whole way to the second point

Any suggestions or a workaround?

Was it helpful?

Solution

If P2 is outside the visible area (480x800 in this particular case) then calculate the intersection of this line with border, an then use the intersection point instead of P2.

OTHER TIPS

You can scale down your line, with something like this:

int maxX = 400;
int maxY = 800;

//Calculate how much we have to scale down to fit in the bounds:
float scaleX = (maxX - p1.x)/p2.x;
float scaleY = (maxY - p1.y)/p2.y;

//Get the smallest scale, so that we fit in both axises.
float scale = Math.min(scaleX, scaleY);

//Only scale if we are scaling down. There is no need to make lines smaller than the screen scale up to the screen bounds:
if(scale < 1.0f){
    p2.x *= scale;
    p2.y *= scale;
}

I haven't tried this, so I can't guarantee that it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top