Q: How to find how many pixel long a line is I am drawing lines using drawLine method in Java. I know beginning and end points of lines. 


*

*How can I measure the length of lines as pixels? 

*If I am working on a BufferedImage whose width and height are determined by Dimension(500,500), do the points which determine the start and the end of a line correspond to the pixels? Then will calculating only the distance of two points be sufficient for measuring the pixel length of line?


A: The BufferedImage works in an integer; Cartesian space. So each pixel corresponds to a point.
Use the distance formula to determine the length, if you have the points. 
int distance = (int) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));


If you are using the Point2D class for your points, there are multiple methods to make this easier for you to determine distance.


*

*double distance(double px, double py)


*

*Returns the distance from this Point2D to a specified point.


*static double distance(double x1, double y1, double x2, double y2)


*

*Returns the distance between two points.


*double distance(Point2D pt)


*

*Returns the distance from this Point2D to a specified Point2D.


*double distanceSq(double px, double py)


*

*Returns the square of the distance from this Point2D to a specified point.


*static double distanceSq(double x1, double y1, double x2, double y2)


*

*Returns the square of the distance between two points.


*double distanceSq(Point2D pt)


*

*Returns the square of the distance from this Point2D to a specified Point2D.


