gpt4 book ai didi

java - 根据点找线

转载 作者:行者123 更新时间:2023-12-01 15:42:57 25 4
gpt4 key购买 nike

我还有问题,我想向你展示我的代码。如果你能运行它,那就太好了。这有点复杂。也许当你看到所有这些时,你可能会发现另一个错误,导致找到错误的行。仍然给出最后一行作为最接近的一行。另一个问题是,当您在创建一行后运行程序时,jmenu 会重复自身,但我可以在第二个 jmenu 上画一条线。我相信这与我的编码无关。你能看一下我的代码吗?感谢您之前的回答。抱歉我的帖子,这是我第一次提问。

  import java.awt.Color;
import java.awt.Point;

public class Lines {

public int id;
public Point point1;
public Point point2;
public int[] denklem;

}

第一节课到此结束

   import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;


public class Tester{

public Tester() {
cizer test = new cizer();
JFrame frame = new JFrame();
// frame.setBackground(Color.DARK_GRAY);
frame.setJMenuBar(test.jmb);
frame.add(test.statusBar, BorderLayout.SOUTH );
Container c = frame.getContentPane();
c.add(test);
c.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setVisible(true);
}
public static void main(String[] args) {
Tester a = new Tester();
}
}
class cizer extends JPanel implements MouseMotionListener, MouseListener {

int mouseclicks = 0;
boolean begin = true;
boolean linesegmentci = false;
boolean lineci = false;
boolean chooser = false;
boolean deleter = false;
boolean digerinde = false;
ArrayList<Lines> array = new ArrayList<Lines>();
ArrayList<Lines> array3 = new ArrayList<Lines>();
Devline devarray = new Devline();
ArrayList<Devline> devarray2 = new ArrayList<Devline>();
JMenuBar jmb = new JMenuBar();
JMenu menu = new JMenu("Secenekler");
JMenu menu2 = new JMenu("Renk");
JMenu menu3 = new JMenu("Kalınlık");
JMenu menu4 = new JMenu("Sec");
JMenuItem item = new JMenuItem("Linesegments drawer");
JMenuItem item2 = new JMenuItem("Line drawer");
JMenuItem item3 = new JMenuItem("Black");
JMenuItem item4 = new JMenuItem("Blue");
JMenuItem item5 = new JMenuItem("Red");
JMenuItem item6 = new JMenuItem("Green");
//JMenuItem item13 = new JMenuItem("Hepsinin rengini degistir");
JMenuItem item7 = new JMenuItem("3");
JMenuItem item8 = new JMenuItem("5");
JMenuItem item9 = new JMenuItem("7");
JMenuItem item10 = new JMenuItem("10");
JMenuItem item11 = new JMenuItem("Find Closest line");
//JMenuItem item12 = new JMenuItem("Sil");
JLabel statusBar = new JLabel( "Mouse outside JPanel" );
Lines l1 = new Lines();
int minValue = Integer.MAX_VALUE;
Lines min = null;
ArrayList<Integer> minimum = new ArrayList<Integer>();
Graphics2D g2D;
Color color;
BasicStroke stroke;
int minx = 0;
boolean deneme = false;
public cizer(){
menu.add(item);
menu.add(item2);
menu2.add(item3);
menu2.add(item4);
menu2.add(item5);
menu2.add(item6);
// menu2.add(item13);
menu3.add(item7);
menu3.add(item8);
menu3.add(item9);
menu3.add(item10);
menu4.add(item11);
// menu.add(item12);
jmb.add(menu);
jmb.add(menu2);
jmb.add(menu3);
jmb.add(menu4);
l1.point1 = new Point();
l1.point2 = new Point();
// min.point1 = new Point();
// min.point2 = new Point();
l1.denklem = new int[3];
stroke = new BasicStroke();


item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
linesegmentci = true;
lineci = false;
chooser = false;
deleter = false;
}
});
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
begin = true;
lineci = true;
linesegmentci = false;
chooser = false;
deleter = false;
}
});
item3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
color = Color.BLACK;


}
});
item4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
color = Color.BLUE;

}
});
item5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
color = Color.RED;

}
});
item6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
color = Color.GREEN;

}
});
item7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stroke = new BasicStroke(3.f);


}
});
item8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stroke = new BasicStroke(5.f);

}
});
item9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stroke = new BasicStroke(7.f);

}
});
item10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stroke = new BasicStroke(10.f);

}
});
item11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chooser = true;
linesegmentci = false;
lineci = false;
deleter = false;

}
});
/* item12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
deleter = true;
chooser = false;

}
});*/
/* item13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
deneme = true;
}
});*/
addMouseListener(this);
addMouseMotionListener(this);

}

public void paint(Graphics g) {

g2D = (Graphics2D) g;
devarray.segmentim.add(l1);

if(linesegmentci == true){
Iterator<Lines> itr = devarray.segmentim.iterator();
while(itr.hasNext()){
Lines l = itr.next();
g.setColor(color);
g2D.setStroke(stroke);
g2D.drawLine(l.point1.x,l.point1.y,l.point2.x,l.point2.y);


}
devarray2.add(devarray);

}
else if(lineci == true){
array3.add(l1);
g2D.setPaint(color);
g2D.setStroke(stroke);
g2D.drawLine(l1.point1.x,l1.point1.y,l1.point2.x,l1.point2.y);
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
if(chooser == true){
Iterator<Lines> itr2 = array.iterator();
while(itr2.hasNext()){
Lines l2 = itr2.next();
minx = (int) (Math.abs((l2.denklem[0] * e.getX()) + (l2.denklem[1] * e.getY()) +
l2.denklem[2]) / (Math.sqrt((l2.denklem[0] *l2.denklem[0]) + (l2.denklem[1] * l2.denklem[1]) )));

if (minx < minValue){
minValue = minx;
min = l2;
}
}
System.out.println("CLOSEST LINE COORDINATES");
System.out.println(min.point1.x);
System.out.println(min.point1.y);
}
else if(linesegmentci == true){

if(begin == true){
if(mouseclicks == 0){
l1.point1.x = e.getX();
l1.point1.y = e.getY();

statusBar.setText( String.format( "Clicked at [%d, %d]",
e.getX(), e.getY() ) );

mouseclicks++;
}
else if(mouseclicks == 1){
l1.point2.x = e.getX();
l1.point2.y = e.getY();
statusBar.setText( String.format( "Clicked at [%d, %d]",
e.getX(), e.getY() ) );
mouseclicks = 0;
begin = false;
repaint();

}
// array2.devarray.add(l1);
array.add(l1);
int a = l1.point1.y - l1.point2.y;
int b = l1.point2.x - l1.point1.x;
int c = (l1.point1.x * l1.point2.y) - (l1.point2.x * l1.point1.y);
l1.denklem[0] = a;
l1.denklem[1] = b;
l1.denklem[2] = c;
}
else{
l1.point1.x = l1.point2.x;
l1.point1.y = l1.point2.y;
l1.point2.x = e.getX();
l1.point2.y = e.getY();
statusBar.setText( String.format( "Clicked at [%d, %d]",
e.getX(), e.getY() ) );
repaint();
// array2.devarray.add(l1);
array.add(l1);
int a = l1.point1.y - l1.point2.y;
int b = l1.point2.x - l1.point1.x;
int c = (l1.point1.x * l1.point2.y) - (l1.point2.x * l1.point1.y);
l1.denklem[0] = a;
l1.denklem[1] = b;
l1.denklem[2] = c;
}
}
else if(lineci == true){

if(mouseclicks == 0){
l1.point1.x = e.getX();
l1.point1.y = e.getY();
statusBar.setText( String.format( "Clicked at [%d, %d]",
e.getX(), e.getY() ) );
mouseclicks++;
}
else if(mouseclicks == 1){
l1.point2.x = e.getX();
l1.point2.y = e.getY();
statusBar.setText( String.format( "Clicked at [%d, %d]",
e.getX(), e.getY() ) );
mouseclicks = 0;
// begin = false;
repaint();

}
array.add(l1);
int a = l1.point1.y - l1.point2.y;
int b = l1.point2.x - l1.point1.x;
int c = (l1.point1.x * l1.point2.y) - (l1.point2.x * l1.point1.y);
l1.denklem[0] = a;
l1.denklem[1] = b;
l1.denklem[2] = c;

}
}

public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) {}

}

抱歉我也忘了这个

   import java.util.ArrayList;


public class Devline {
ArrayList<Lines> segmentim = new ArrayList<Lines>();

}

最佳答案

我认为您的解决方案比需要的要复杂一些。就我个人而言,我会选择具有以下结构的东西:

  Lines min = null;
double minDist = Double.MAX_VALUE;
for (Lines l : array) {
double dist = ...; // compute the distance from the point to `l'
if (dist < minDist) {
minDist = dist;
min = l;
}
}

附注我没有检查你的距离计算。

关于java - 根据点找线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7713100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com