Android 将for循环中的变量指定给arraylist中每个点的x坐标
我试图将我的Android 将for循环中的变量指定给arraylist中每个点的x坐标,android,for-loop,arraylist,coordinates,variable-assignment,Android,For Loop,Arraylist,Coordinates,Variable Assignment,我试图将我的ArrayList中的每个x和y坐标分配给一个变量,因为每个坐标将与用户在屏幕上单击的点的坐标进行比较 // the coordinates of each point Point p1 = new Point(100, 100); Point p2 = new Point(100, 400); Point p3 = new Point(400, 100); Point p4 = new Point(400, 400); // the
ArrayList
中的每个x和y坐标分配给一个变量,因为每个坐标将与用户在屏幕上单击的点的坐标进行比较
// the coordinates of each point
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 400);
Point p3 = new Point(400, 100);
Point p4 = new Point(400, 400);
// the points are added to the arraylist
mPoints.add(p1);
mPoints.add(p2);
mPoints.add(p3);
mPoints.add(p4);
}
//this is the content of my arraylist
@Override
public boolean onTouchEvent(MotionEvent event)
{
//motionevent detects motion from the user
//float x;
// x = event.getX();
//float y;
//y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_UP:
//drawLine(x, y);
//mContext = this.mContext;
float Cox = event.getX();
float Coy = event.getY();
Double nearestDistance = 1000.12; //this is hardcoded for the sake of
// declaring the variable.
int NearestPoint = -1;
// int NearestPoint = mPoints.get() ;
for (int i = 0; i<mPoints.size(); i++)
{
float xi;
float yi;
xi = mPoints.x;
yi = mPoints.y;
}
//the assignments above are my problem
//每个点的坐标
点p1=新点(100100);
点p2=新点(100400);
点p3=新点(400100);
点p4=新点(400400);
//这些点将添加到arraylist中
mPoints.add(p1);
mPoints.add(p2);
mPoints.add(p3);
mPoints.add(p4);
}
//这是我的arraylist的内容
@凌驾
公共布尔onTouchEvent(运动事件)
{
//motionevent检测来自用户的运动
//浮动x;
//x=event.getX();
//浮动y;
//y=event.getY();
开关(event.getAction())
{
case MotionEvent.ACTION\u UP:
//抽绳(x,y);
//mContext=this.mContext;
float-Cox=event.getX();
float Coy=event.getY();
Double nearestDistance=1000.12;//这是为了
//声明变量。
int最近点=-1;
//int NearestPoint=mPoints.get();
for(int i=0;i如果我正确理解了您的意思,那么for循环需要如下所示
for (int i = 0; i<mPoints.size(); i++)
{
float xi = mPoints.get(i).x;
float yi = mPoints.get(i).y;
System.out.println("Point " + i + " y: " + yi);
System.out.println("Point " + i + " x: " + xi);
}
<代码> >(int i=0;用“比较”来区分你的意思)。你想如何比较它们?我将有一个基本上模仿欧几里德距离公式的方法。所以变量通过的是Xi,Yi,x,y(席席的坐标,Yi将是用户点击的点,X,Y的坐标是数组中的点)。