How to find how many pixel long a line is

时间:2015-11-12 12:05:44

标签: java java-2d

I am drawing lines using drawLine method in Java. I know beginning and end points of lines.

  1. How can I measure the length of lines as pixels?
  2. 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?

1 个答案:

答案 0 :(得分:1)

BufferedImage以整数形式工作;笛卡尔空间。所以每个像素对应一个点。

如果您有积分,请使用distance formula确定长度。

int distance = (int) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

如果您使用Point2D课程作为积分,有多种方法可以让您更轻松地确定距离。