62,616
社区成员
发帖
与我相关
我的任务
分享
int x1 = 20, y1 = 50, x2 = 300, y2 = 600;
List<Point> list = new ArrayList<Point>();
if (x1 == x2) {
// Tangent = NaN
int from = Math.min(y1, y2);
int to = Math.max(y1, y2);
for (int y = from; y <= to; y++) {
list.add(new Point(x1, y));
}
} else {
double slope = ((double) (y2 - y1)) / ((double) (x2 - x1));
int step = (x2 > x1) ? 1 : -1;
for (int x = x1; x != x2; x += step) {
int y = (int)((x - x1) * slope + y1);
list.add(new Point(x, y));
}
}
System.out.println(list);
Graphics2D g2d = (Graphics2D) g;
// 关闭抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
BufferedImage img = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
Color color = Color.BLACK;
g.setColor(color);
g.drawLine(20, 50, 300, 600);
int[] rgbArray = new int[img.getWidth()];
List<Point> list = new ArrayList<Point>();
for (int y = 0; y < img.getHeight(); y++) {
img.getRGB(0, y, img.getWidth(), 1, rgbArray, 0, img.getWidth());
for (int x = 0; x < rgbArray.length; x++) {
if (rgbArray[x] == color.getRGB()) {
list.add(new Point(x, y));
}
}
}
System.out.println(list);