trackeditor: fix segment selection

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@8687 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 7dae99bb8d62c11266a8366f73a58439578a5af4
Former-commit-id: 83f063c1fed02fce6a944ba88a1b6621dbe697be
This commit is contained in:
iobyte 2023-01-20 20:31:56 +00:00
parent 3804333b21
commit 2ca621b152
4 changed files with 28 additions and 41 deletions

View file

@ -2435,7 +2435,7 @@ public class EditorFrame extends JFrame
int type = JOptionPane.PLAIN_MESSAGE;
String msg = Editor.getProperties().title + " " + Editor.getProperties().version + "\n\n"
+ "Copyright Charalampos Alexopoulos\n"
+ "Copyright (2022) Robert Reif\n";
+ "Copyright (2022-2023) Robert Reif\n";
JOptionPane.showMessageDialog(null,msg,"About",type);
}
}

View file

@ -433,7 +433,7 @@ public class CircuitView extends JComponent implements KeyListener, MouseListene
oldShape.setArcDeg(tmpAngle);
oldShape.setRadiusEnd(tmpRadius - deltaRadiusStep);
redrawCircuit();
if (oldShape.contains(mousePoint.x, mousePoint.y))
if (oldShape.contains(mousePoint))
{
splitPoint = curStep / nbSteps;
break;
@ -489,7 +489,7 @@ public class CircuitView extends JComponent implements KeyListener, MouseListene
oldShape.setArcDeg(tmpAngle);
oldShape.setRadiusEnd(tmpRadius - deltaRadiusStep);
redrawCircuit();
if (oldShape.contains(mousePoint.x, mousePoint.y))
if (oldShape.contains(mousePoint))
{
splitPoint = curStep / nbSteps;
break;
@ -528,7 +528,7 @@ public class CircuitView extends JComponent implements KeyListener, MouseListene
newShape3.setLength(length * (1.0 - splitPoint));
oldShape.setLength(length * splitPoint);
redrawCircuit();
if (oldShape.contains(mousePoint.x, mousePoint.y))
if (oldShape.contains(mousePoint))
{
break;
}
@ -1132,7 +1132,7 @@ public class CircuitView extends JComponent implements KeyListener, MouseListene
Segment obj = i.next();
if (Class.forName("utils.circuit.Segment").isAssignableFrom(obj.getClass())
&& obj.contains(mousePoint.getX(), mousePoint.getY()))
&& obj.contains(mousePoint))
{
// object found !
out = obj;
@ -1162,7 +1162,7 @@ public class CircuitView extends JComponent implements KeyListener, MouseListene
try
{
if (Class.forName("utils.circuit.Segment").isAssignableFrom(object.getClass())
&& object.contains(mousePoint.getX(), mousePoint.getY()))
&& object.contains(mousePoint))
{
// object found !
out = object;

View file

@ -34,7 +34,7 @@ public class Properties
private static Properties instance = new Properties();
private Vector<ActionListener> propertiesListeners = new Vector<ActionListener>();
public final String title = "sd2-trackeditor";
public final String version = "1.0.0";
public final String version = "1.0.1";
private String path;
private double imageScale = 1;

View file

@ -199,56 +199,43 @@ public class Segment implements Cloneable
return shape;
}
public boolean contains(double x, double y)
// adapted from: https://web.archive.org/web/20130126163405/http://geomalgorithms.com/a03-_inclusion.html
// Copyright 2000 softSurfer, 2012 Dan Sunday
private double isLeft(Point2D.Double P0, Point2D.Double P1, Point2D.Double P2)
{
return ((P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y));
}
public boolean contains(Point2D.Double point)
{
for (int i = 0; i < points.length; i += 4)
{
boolean found = true;
int count = 0;
for (int j = 0; j < 4; j++)
{
int idxA = i + j;
int idxB = i + (j + 1) % 4;
int idxC = i + (j + 2) % 4;
int idxD = i + (j + 3) % 4;
int start = i + j;
int next = i + ((j + 1) % 4);
if (points[idxA].equals(points[idxB]) || points[idxB].equals(points[idxC])
|| points[idxC].equals(points[idxD]) || points[idxD].equals(points[idxA]))
if (points[start].y <= point.y)
{
found = false;
break;
}
if (points[idxA].x == points[idxB].x)
{
// vertical line
int localSign = (x > points[idxA].x ? 1 : -1);
int localSign2 = (points[idxC].x > points[idxA].x ? 1 : -1);
if (localSign != localSign2)
if (points[next].y > point.y)
{
found = false;
break;
if (isLeft(points[start], points[next], point) > 0)
++count;
}
} else
}
else
{
double a, b;
a = (points[idxB].y - points[idxA].y) / (points[idxB].x - points[idxA].x);
b = points[idxA].y - a * points[idxA].x;
int localSign = (y > a * x + b ? 1 : -1);
int localSign2 = (points[idxC].y > a * points[idxC].x + b ? 1 : -1);
if (localSign != localSign2)
if (points[next].y <= point.y)
{
found = false;
break;
if (isLeft(points[start], points[next], point) < 0)
--count;
}
}
}
if (found)
if (count != 0)
return true;
}