How to choose a row

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

  • A collection of lines, each line has two endpoints (one for starting and one for ending the line).
  • Lines are drawn correctly on canvas
  • Mouse click events are accepted when you click on the canvas, so I can get the x and y coordinates of the mouse pointer

I know that I can iterate over a list of strings, but I have no idea how to build an algorithm to select a string at a given coordinate (i.e., a mouse click). Has anyone got any ideas or pointed me in 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;
}
+2
source share
4

- . , , . , , . , ( - , )

// 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;

}

+6

2D api, .

Line2D.Double . Line2D.Double contains(), , .

+4
+4

, - ... java.awt.geom.Line2D:

public boolean (double x,                         double y)

, Line2D. , Line2D false, .

:          Shape

:         x - X ,         y - Y , .

:         false, Line2D .

:         1.2

Tojis

0

Source: https://habr.com/ru/post/1725062/


All Articles