Determin if line is in view

Hi :slight_smile:

I got to draw a big star map, with a lot jumps (lines). normally only a few of the jumps will be in view. So, I want to check if the line is inside the view before I draw it. But I can’t seem to get the code right.

So, how do I check it my line is inside the view or not?
It has to be drawn if any part of the line is inside view

public class GalaxyView extends JPanel{
	private void paintJump(Graphics graphics, Point p1, Point p2) {
		graphics.drawLine(p1.x, p1.y, p2.x, p2.y);
	}
}

Hope I made sense…

/GoldenGnu

I found a solution. :slight_smile:

If anyone else get the same problem:

public class GalaxyView extends JPanel{
	private void paintJump(Graphics graphics, Point p1, Point p2) {
		Line2D theLine = new Line2D.Double(p2.getX(), p2.getY(), p1.getX(), p1.getY() );
		
		//Only draw line if it's inside view
		if (theLine.intersects(0, 0, this.getWidth(), this.getHeight() ) ){
			graphics.drawLine(p1.x, p1.y, p2.x, p2.y);
		}
	}
}