public void point1(){
int x2 = 0, y2 = 78, x1 = 230, y1 = 560;
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 mul= ((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) * mul+ y1);
list.add(new Point(x, y));
}
}
for (Point point : list) {
System.out.println("x="+point.getX()+",y="+point.getY());
}
}
方法二:通过分析像素点
public void point(int x1,int y1,int x2,int y2){
if(x1<0 || y1<0 || x2<0 || y2<0){
System.out.println("**********坐标点不能为负数************");
return;
}
//最大的宽和高
int w = x1>x2 ? x1 : x2;
int h = y1>y2 ? y1 : y2;
BufferedImage img = new BufferedImage(w+1, h+1, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
Color color = Color.BLACK;
g.setColor(color);
g.drawLine(x1,y1,x2,y2);//绘制一黑色直线
//判断正方向
byte dir = 0;//表示反方向
if(y2>y1){
dir = 1;
}else if(y2==y1){
if(x2>x1){
dir = 1;
}
}
int[] rgbArray = new int[img.getWidth()];
List<Point> list = new ArrayList<Point>();
if(dir==1){
for (int y = 0; y < img.getHeight(); y++) {
//0,y图片开始的坐标点左边开始 img.getWidth(),宽度 1=去的高度,这里一次取一行,
//起始X,Y 区域宽和高,像素点数组,数组中偏移量,扫描的行间距
img.getRGB(0, y, img.getWidth(), 1, rgbArray, 0, img.getWidth());//0,y
for (int x = 0; x < rgbArray.length; x++) {
if (rgbArray[x] == color.getRGB()) {//如果等于指定的颜色
list.add(new Point(x, y));
}
}
}
}else{
for (int y = img.getHeight()-1; y >= 0; y--) {
//0,y图片开始的坐标点左边开始 img.getWidth(),宽度 1=去的高度,这里一次取一行,
//起始X,Y 区域宽和高,像素点数组,数组中偏移量,扫描的行间距
img.getRGB(0, y, img.getWidth(), 1, rgbArray, 0, img.getWidth());//0,y
for (int x = rgbArray.length-1; x >= 0; x--) {
if (rgbArray[x] == color.getRGB()) {//如果等于指定的颜色
list.add(new Point(x, y));
}
}
}
}
for (Point point : list) {
System.out.println("x="+point.getX()+",y="+point.getY());
}
// System.out.println("x="+list.get(index));
}
方法三:算法,有些笨
public static Vector<int[]> getPoint(int x1,int y1,int x2,int y2){
Vector<int[]> path = new Vector<int[]>();
int x0 = x2-x1;
int y0 = y2-y1;
if(x0==0 && y0 == 0){
return null; //相同点
}
int opX = x0 > 0 ? 1:-1;
int opY = y0 > 0 ? 1:-1;//符号
int absX0 = x0>0 ? x0 :-x0;
int absY0 = y0>0 ? y0 :-y0;//取绝对值
if(x0==0){//同一竖直线
for (int i = 0; i < absY0+1; i++) {
path.add(new int[]{x1,y1});
y1 += opY;
}
for (int[] ds : path) {
System.out.println(ds[0]+"="+ds[1]);
}
return path;
}
if(y0==0){//同水平直线
for (int i = 0; i < absX0+1; i++) {
path.add(new int[]{x1,y1});
x1 += opX;
}
for (int[] ds : path) {
System.out.println(ds[0]+"="+ds[1]);
}
return path;
}
//****************************以上为同点 同水平 同竖直线 的情况**************************************
//****************************以下为有任意斜度情况*******************************************************
double mul = 1;
int xIndex = 1;
int yIndex = 1; //为1的方向 按倍数加
int forCount = 0; //循环的次数
if(absX0>absY0){ //倍数
mul = absX0/(absY0*1.0);
yIndex = 0;
forCount = absY0;
// y1 = y1+1*opY;
}else if(absX0<absY0){
mul = absY0/(absX0*1.0);
xIndex = 0;
forCount = absX0;
}
double mod = mul;
mul = Math.floor(mul);
mod -= mul;//保留舍去的数 累计修正
mod = mod>0 ? mod :-mod;
double count = 1.0;
double totalMod = 0.0;
while(forCount>0){//
int p[] = new int[2];
if(count<mul){
if(xIndex==1){
p[0] = x1+1*opX;
x1 = p[0];
}
if(yIndex==1){
p[1] = y1+1*opY;
y1 = p[1];
}
count++;
}else{
// if(xIndex==0){
p[0] = x1+1*opX;
x1 = p[0];
// }
// if(yIndex==0){
p[1] = y1+1*opY;
y1 = p[1];
// }
count = 1;
forCount--;//记录一次循环
}
totalMod+=mod; //误差修正
path.add(new int[]{x1,y1});//添加到集合中
//误差修正
if(totalMod>1){
totalMod = totalMod-1;
// count--;
if(xIndex==1){
p[0] = x1+1*opX;
x1 = p[0];
}
if(yIndex==1){
p[1] = y1+1*opY;
y1 = p[1];
}
count++;
path.add(new int[]{x1,y1});//添加到集合中
// continue;
}
if(x1==x2 && y1==y2){
break;
}
}
// for (int[] ds : path) {
// System.out.println(ds[0]+"="+ds[1]);
// }
return path;
}