如何选择一条线路 [英] How to select a line

查看:194
本文介绍了如何选择一条线路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图找出如何实现选择线或边在绘图区域的方法,但我的数学是有点欠缺。这是我到目前为止有:

  • 行的集合,每行有两个端点(一个开始,一个结束行)
  • 在该行正确在画布上绘制
  • 在鼠标点击点击画布事件时收到,这样我就可以得到x和鼠标指针的y坐标

我知道我可以通过行列表循环,但我不知道如何构造一种算法来选择线路由给定的坐标(即点击鼠标)。任何人有任何意见或点我朝着正确的方向?

  //进口java.awt.Point中

公共线路selectLine(点mousePoint){
    对(线L:getLines()){
        点开始= l.getStart();
        点结束= l.getEnd();
        如果(canSelect(开始,结束,mousePoint)){
            返回L; //发现线!
        }
    }
    返回null; //找不到线mousePoint
}

公共布尔canSelect(点开始,点结束,点selectAt){
    //我该怎么做呢?
    返回false;
}
 

解决方案

要做到这一点最好的办法是用线相交的方法。像其他用户提到的,你需要有各地,他们点击一个缓冲区。因此,创建一个在你的鼠标的坐标为中心的矩形,然后测试矩形相交伴您行。下面是一些code应该工作(没有一个编译器或任何东西,但应该很容易修改)

  //宽度和鼠标左右矩形区域的高度
//指针用于点击检测上线
私有静态最终诠释HIT_BOX_SIZE = 2;



公共无效鼠标pressed(的MouseEvent E){
    INT X = e.getX();
    INT Y = e.getY();

    的Line2D clickedLine = getClickedLine(X,Y);
}


/ **
*返回线的集合中的第一行
*足够接近,用户点击后,如果空
*没有这样的行存在
*
* /

公众的Line2D getClickedLine(INT X,int y)对{
INT BOXX = X  -  HIT_BOX_SIZE / 2;
INT四四方方= Y  -  HIT_BOX_SIZE / 2;

INT宽度= HIT_BOX_SIZE;
INT高= HIT_BOX_SIZE;

对于(的Line2D行:getLines()){
如果(line.intersects(BOXX,四四方方,宽,高){
回线;
}
}
返回null;
 

}

So I'm trying to figure out how to implement a method of selecting lines or edges in a drawing area but my math is a bit lacking. This is what I got so far:

  • A collection of lines, each line has two end points (one to start and one to end the line)
  • The lines are drawn correctly on a canvas
  • Mouse clicks events are received when clicking the canvas, so I can get the x and y coordinate of the mouse pointer

I know I can iterate through the list of lines, but I have no idea how to construct an algorithm to select a line by a given coordinate (i.e. the mouse click). Anyone got any ideas or point me to the right direction?

// import java.awt.Point

public Line selectLine(Point mousePoint) {
    for (Line l : getLines()) {
        Point start = l.getStart();
        Point end = l.getEnd();
        if (canSelect(start, end, mousePoint)) {
            return l; // found line!
        }
    }
    return null; // could not find line at mousePoint
}

public boolean canSelect(Point start, Point end, Point selectAt) {
    // How do I do this?
    return false;
}

解决方案

Best way to do this is to use the intersects method of the line. Like another user mentioned, you need to have a buffer area around where they clicked. So create a rectangle centered around your mouse coordinate, then test that rectangle for intersection with your line. Here's some code that should work (don't have a compiler or anything, but should be easily modifiable)

// Width and height of rectangular region around mouse
// pointer to use for hit detection on lines
private static final int HIT_BOX_SIZE = 2;



public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    Line2D clickedLine = getClickedLine(x, y);
}


/**
* Returns the first line in the collection of lines that
* is close enough to where the user clicked, or null if
* no such line exists
*
*/

public Line2D getClickedLine(int x, int y) {
int boxX = x - HIT_BOX_SIZE / 2;
int boxY = y - HIT_BOX_SIZE / 2;

int width = HIT_BOX_SIZE;
int height = HIT_BOX_SIZE;

for (Line2D line : getLines()) {
	if (line.intersects(boxX, boxY, width, height) {
		return line;
	}		
}
return null;

}

这篇关于如何选择一条线路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆