
正文
Java三维空间代码 java定义三维数组并赋值
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求Java大神编写程序
Java参考源代码:
public class Test {
public static void main(String[] args) {
Point p = new Point(1, 2, 3);
Point p0 = new Point(0, 0, 0);
Point q = new Point(1, 3, 0);
System.out.println("x:" + p.getX());
System.out.println("y:" + p.getY());
System.out.println("z:" + p.getZ());
System.out.println("distance:" + p.distance(p0));
p.setX(5);
p.setY(4);
p.setZ(0);
System.out.println("change x y z");
System.out.println("x:" + p.getX());
System.out.println("y:" + p.getY());
System.out.println("z:" + p.getZ());
System.out.println("distance:" + p.distance(p0));
System.out.println("p to q distance:" + p.distance(q));
}
}
class Point {
protected double x;
protected double y;
protected double z;
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return this.x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return this.y;
}
public void setZ(double z) {
this.z = z;
}
public double getZ() {
return this.z;
}
public double distance(Point p) {
return (this.x - p.getX()) * (this.x - p.getX())
+ (this.y - p.getY()) * (this.y - p.getY())
+ (this.z - p.getZ()) * (this.z - p.getZ());
}
}
运行结果:
相关问答
Q1: 用java分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点怎么做?
/*
* Project: 空间点
* Author: searchpcc(xixi,11级,20112866)
* File: TuXing.java
* Instruction: 主程序
* Time: 2012-11-07
*/
class Point2D{
int x;
int y;
public Point2D(){
}
public Point2D(int x, int y){
this.x = x;
this.y = y;
}
public void offset(int a, int b){
x = a;
y = b;
this.x = this.x + 1;
this.y = this.y + 1;
System.out.print(x);
System.out.print(y);
}
double distan(Point2D p1, Point2D p2){
return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
}
class Point3D extends Point2D{
int z;
public Point3D(int x, int y, int z){
super(x, y);
this.z = z;
}
public Point3D(){
}
public Point3D(Point2D p, int z){
super(p.x, p.y);
this.z = z;
}
public void offset(int a, int b,int c){
x = a;
y = b;
z = c;
this.x = this.x + 1;
this.y = this.y + 1;
this.z = this.z + 1;
}
double distan(Point3D p1, Point3D p2){
return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z)));
}
}
public class KongJian1{
public static void main(String[] args){
Point2D p1 = new Point2D(3,3);
p1.offset(3, 3);
Point2D p2 = new Point2D(2,2);
Point3D p3 = new Point3D(4,4,4);
p3.offset(4,2,3);//可以进行移动
Point3D p4 = new Point3D(5,5,5);
System.out.println("p1和p2之间的距离为:");
System.out.println(new Point2D().distan(p1,p2));
System.out.println("p3和p4间的距离为:");
System.out.println(new Point3D().distan(p3,p4));
}
}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
Q2: java基础,有没有大神来给我讲一下这篇代码
Point是一个三维空间Java三维空间代码的点
Java三维空间代码他有三个成员变量, x y z 表示他的 x y z 三个轴的坐标
构造方法初始化Java三维空间代码了这个点的位置
distance方法 , 是计算两个点的距离 , 公式是数学公式 , 就是两个点的三个坐标各自求差, 然后平方 , 结果相加然后再开方
Q3: 编程序:已知三维空间中的一个点坐标(x,y,z),求该点到原点的距离
三维空间距离公式是√[(x1-x2)^2+(y1-y2)^2+(z1-z2)^2]
那么任意一点到原点距离公式d = √(x^2+y^2+z^2)
这边是JavaJava三维空间代码的代码Java三维空间代码,其Java三维空间代码他的也差不多是这样。
假设已存在已知变量double x, y, z
代码如下
double sum = x*x + y*y + z*z;
double distance = Math.pow(sum, 2);
定义
三维空间是指点的位置由三个坐标决定的空间Java三维空间代码,具有长度、宽度和高度。
空间和时间是运动着的物质的存在形式。空间是物质存在的广延性,时间是物质运动过程的持续性和顺序性。同物质一样,空间和时间是不依赖人的意识而存在的客观存在,是永恒的。空间、时间同运动着的物质是不可分割的,没有脱离物质运动的时空,也没有不在时空中运动的物质。但时空描述和量度是相对的。
Q4: JAVA题目求助
1.
public class MyPoint {
private int x;
private int y;
private int z;
public MyPoint(){
this.x=0;
this.y=0;
this.z=0;
}
public MyPoint(int x1,int y1,int z1){
this.x=x1;
this.y=y1;
this.z=z1;
}
public void set_xyz(int xx,int yy,int zz){
this.x=xx;
this.y=yy;
this.z=zz;
}
public int dToZero(){
return x*x+y*y+z*z;
}
public int dToPoint(int a,int b,int c){
return (x-a)*(x-a)+(y-b)*(y-b)+(z-c)*(z-c);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyPoint p1 = new MyPoint(3,4,5);
MyPoint p2 = new MyPoint();
p2.set_xyz(1, 3, 8);
System.out.println("p1(3,4,5)到原点距离:"+p1.dToZero());
System.out.println("p2(1,3,8)到原点距离:"+p2.dToZero());
System.out.println("p1(3,4,5)到p2(1,3,8)距离:"+p1.dToPoint(p2.getX(), p2.getY(), p2.getZ()));
}
}
====================================================================== 2.
public class MyInt {
private int Prime;
public MyInt() {
}
public MyInt(int prime) {
this.Prime=prime;
}
public void PrintPrime(){
int count = 0;
for(int i=1;i=this.Prime;i++){
boolean isPrime = true;
for(int j=2;j=i/2;j++){
if(i%j==0){
isPrime = false;
break;
}
}
if(isPrime){
System.out.print(i+" ");
count++;
if(count%5==0){
System.out.println();
}
}
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
MyInt myInt = new MyInt(100);
myInt.PrintPrime();
}
}
Q5: 用java语句写出三维空间的点,例子如下:麻烦帮我加上注释~~各位大哥大姐帮忙啦
public class Point {
private double x;
private double y;
private double z;
//无参数默认原点
public Point() {
this(0, 0, 0);
}
//构造方法指定坐标
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public static void main(String args[]){
Point p = new Point(1, 1, 1);//构造,1,1,1点,距离远点距离是根号3,1。732。。。
System.out.println("Before new valued set, the point is: " + p.toString());
System.out.println("Before new valued set, the distance to (0, 0, 0) is: " + p.getDistance());
p.setPosition(2, 2, 2);//重新设置点坐标
System.out.println("After new valued set, the point is: " + p.toString());
System.out.println("After new valued set, the distance to (0, 0, 0) is: " + p.getDistance());
}
public void setX(double x) {//设置x坐标
this.x = x;
}
public void setY(double y) {//设置y坐标
this.y = y;
}
public void setZ(double z) {//设置z坐标
this.z = z;
}
public void setPosition(double x, double y, double z){//一次性设置三个坐标的方法
setX(x);
setX(y);
setX(z);
}
// 计算距离 x*x + y*y+z*x,然后开平方
public double getDistance(){
final int square = 2;//平方
return Math.sqrt(Math.pow(x, square) + Math.pow(y, square)+ Math.pow(z, square));
}
public String toString(){//重写输出方法
return "(x, y, z): " + x + "," + y + "," + z;
}
}
-------------
Before new valued set, the point is: (x, y, z): 1.0,1.0,1.0
Before new valued set, the distance to (0, 0, 0) is: 1.7320508075688772
After new valued set, the point is: (x, y, z): 2.0,1.0,1.0
After new valued set, the distance to (0, 0, 0) is: 2.449489742783178
Java三维空间代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java定义三维数组并赋值、Java三维空间代码的信息别忘了在本站进行查找喔。







