「java手动输入坐标」自定义坐标怎样输入

博主:adminadmin 2022-11-22 18:50:08 67

今天给各位分享java手动输入坐标的知识,其中也会对自定义坐标怎样输入进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java编程:输入n个二维平面上的坐标点,所有的坐标点按照以x轴数为第一关键字,y轴为第二关键字顺

import java.util.Arrays;

import java.util.Scanner;

public class Demo {

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int n = in.nextInt();

MyPoint[] points = new MyPoint[n];

for (int i = 0; i  n; i++) {

points[i] = new MyPoint(in.nextInt(), in.nextInt());

}

Arrays.sort(points);

//System.out.println("排序后:");

for (int i = 0; i  n; i++) {

System.out.println(points[i].x + " " + points[i].y);

}

in.close();

}

}

class MyPoint implements ComparableMyPoint {

public int x;

public int y;

public MyPoint(int x, int y) {

this.x = x;

this.y = y;

}

@Override

public int compareTo(MyPoint other) {

if (this.x  other.x) {

return 1;

} else if (this.x  other.x) {

return -1;

} else if (this.y  other.y) {

return 1;

} else if (this.y  other.y) {

return -1;

}

return 0;

}

}

运行示例:

求用java编写,输入N个点的坐标,判断这N个点能否构成一个凸多边形。有问题可以一起讨论

递归寻找各个点最近的点,两点间距离可通过勾股定理求得。

两个最近的点连成一条直线,

然后判断各条直线的交点是否为输入的那些点。

如果交点均在输入的点处,则是凸多边形。

只要有任意两条线的交点不在输入的点处,就非凸多边形。但是要排除多点一线的情况。

求两条线交点可有参数方程求得。

如下

两点定一线,

线1,点1(x1,y1) 点2 (x2,y2)

线2,点3(x3,y3) 点4 (x4,y4)

t=[(y4-y3)(x3-x1)-(y3-y1)(x4-x3)] ÷ [(y4-y3)(x2-x1)-(y2-y1)(x4-x3)]

注意不要用整数计算,要用浮点数,否则会得不到准确的t值

交点处坐标为

x = x1+(x2-x1)t

y = y1+(y2-y1)t

简单点说,t=0或者1时,两线相交于给定点,任意其他值,均交与其他点。

原理就是这些了,自己写代码吧

java.让用户输入x坐标,和y坐标。当用户输入完x坐标(比如200),敲enter,

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

public class SetLocationFrame extends JFrame{

private JTextField t1,t2;

JLabel l;

int x,y;

public static void main(String[]args){

new SetLocationFrame();

}

public SetLocationFrame(){

super("Where should I be?");

this.setLayout(null);

this.setDefaultCloseOperation(3);

init();

this.setVisible(true);

}

private void init() {

setBounds(100, 100, 300, 200);

x = this.getLocation().x;

y = this.getLocation().y;

JLabel lblNewLabel = new JLabel("Enter new X here");

lblNewLabel.setBounds(44, 10, 136, 20);

add(lblNewLabel);

JLabel lblEnterNewY = new JLabel("Enter new Y here");

lblEnterNewY.setBounds(44, 40, 136, 20);

add(lblEnterNewY);

t1 = new JTextField(x+"");

t1.setBounds(190, 10, 66, 21);

add(t1);

t1.setColumns(10);

t1.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

x = Integer.parseInt(t1.getText());

l.setText("x is "+ x +" and y is "+y);

t2.requestFocus();

}

});

t2 = new JTextField(y+"");

t2.setColumns(10);

t2.setBounds(190, 40, 66, 21);

add(t2);

t2.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

y = Integer.parseInt(t2.getText());

l.setText("x is "+ x +" and y is "+y);

}

});

JButton b = new JButton("Move The Window");

b.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

setLocation(x,y);

System.out.println(x +":" + y);

}

});

b.setBounds(54, 70, 197, 37);

add(b);

JLabel lblComponentOfCurrent = new JLabel("Component of Current Location:");

lblComponentOfCurrent.setBounds(44, 114, 197, 20);

add(lblComponentOfCurrent);

l = new JLabel("x is "+ x +" and y is "+y);

l.setHorizontalAlignment(SwingConstants.CENTER);

l.setBounds(64, 144, 156, 20);

add(l);

}

}

用java语言编写输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。

import

java.util.Scanner;

public

class

TestObject

{

/**

*

@param

args

*/

public

static

void

main(String[]

args)

{

//

TODO

Auto-generated

method

stub

Scanner

in

=

new

Scanner(System.in);

System.out.println("请输入第一个坐标点:");

int

x1

=

in.nextInt();

int

y1

=

in.nextInt();

System.out.println("请输入第二个坐标点:");

int

x2

=

in.nextInt();

int

y2

=

in.nextInt();

int

distance

=

(int)

Math.sqrt(Math.abs((x1

-

x2)*(x1

-

x2))+Math.abs((y1

-

y2)*(y1

-

y2)));

System.out.println("两点间距离是:"+distance);

}

}

JAVA 编程题(输入坐标求出边长和面积以及...)?

四边形的话,坐标点应该是四对八个坐标数字啊,参数x,y,width,height给出没啥意义啊,题意再说清晰一点

java手动输入坐标的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于自定义坐标怎样输入、java手动输入坐标的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。