Java 如何确定坐标点是否为负
我有一个Java 如何确定坐标点是否为负,java,Java,我有一个.txt文件,其中包含以下格式数据(坐标) 我使用以下代码将此数据导入到中 public ArrayList<Point3d> loadFile(String filename) { ArrayList<Point3d> words = new ArrayList<Point3d>(); try { Scanner chopper = new Scanner(new File(filename));
.txt
文件,其中包含以下格式数据(坐标)
我使用以下代码将此数据导入到中
public ArrayList<Point3d> loadFile(String filename) {
ArrayList<Point3d> words = new ArrayList<Point3d>();
try {
Scanner chopper = new Scanner(new File(filename));
while (chopper.hasNext()) {
double x=chopper.nextDouble();
double y=chopper.nextDouble();
double z=chopper.nextDouble();
Point3d p=new Point3d(x, y, z);
words.add(p);
// just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
}
chopper.close();
} catch (Exception e) {
System.out.println(e);
}
return words;
}
我该怎么做。我这里的解决方案使用
映射
数据结构,但是如果您愿意,您可以创建两个列表
在而循环之外添加:
Map<Integer, Point3D> negativeCoord = new HashMap<>();
Map<Integer, Point3D> positivetiveCoord = new HashMap<>();
int currentIndex = 0;
Map negativecord=newhashmap();
Map positiveCoord=新HashMap();
int currentIndex=0;
在它里面:
Point3d p = new Point3d(x, y, z);
if(x < 0 || y < 0 || z < 0) {
negativeCoord.put(currentIndex, p);
} else {
positiveCoord.put(currentIndex, p);
}
currentIndex++;
Point3d p=新的Point3d(x,y,z);
if(x<0 | | y<0 | | z<0){
负序输出(currentIndex,p);
}否则{
正命令put(currentIndex,p);
}
currentIndex++;
这将是一种解决方案,只考虑正面和负面,而不知道它们的顺序
public ArrayList<Point3D> getPositives(ArrayList<Point3D> points) {
ArrayList<Point3D> positives = new ArrayList<>();
for(Point3D next : points) {
if(next.getX() >= 0 && next.getY() >= 0 && next.getZ() >= 0)
posivites.add(next);
}
return positives.
}
public ArrayList<Point3D> getNegatives(ArrayList<Point3D> points) {
ArrayList<Point3D> negatives = new ArrayList<>();
for(Point3D next : points) {
if(next.getX() < 0 || next.getY() < 0 || next.getZ() < 0)
negatives.add(next);
}
return negatives.
}
public ArrayList getPositives(ArrayList点){
ArrayList正片=新的ArrayList();
对于(点3D下一步:点){
如果(next.getX()>=0&&next.getY()>=0&&next.getZ()>=0)
posivites.add(下一步);
}
返回正数。
}
公共ArrayList getNegatives(ArrayList点数){
ArrayList negatives=新的ArrayList();
对于(点3D下一步:点){
if(next.getX()<0 | | | next.getY()<0 | | | next.getZ()<0)
否定。添加(下一步);
}
返回负片。
}
根据您的场景,明智的做法是为信息创建一个自己的容器类
public class PointContainer {
public final Point3D point;
public final int order;
public PointCoordinates (Point3D p, int order) {
this.point = p;
this.order = order;
}
public boolean isNegative() {
return point.getX() < 0 || point.getY() < 0 || point.getZ() < 0;
}
}
公共类PointContainer{
公共终点3D点;
公共秩序;
公共点坐标(点3D p,整数顺序){
该点=p;
这个。顺序=顺序;
}
公共布尔值为负(){
返回点.getX()<0 | | point.getY()<0 | | point.getZ()<0;
}
}
然后用它填充您的列表
public ArrayList<Point3d> loadFile(String filename) {
ArrayList<PointContainer> words = new ArrayList<PointContainer>();
try {
Scanner chopper = new Scanner(new File(filename));
for (int line = 0; chopper.hasNext(); line ++) {
double x=chopper.nextDouble();
double y=chopper.nextDouble();
double z=chopper.nextDouble();
Point3d p=new Point3d(x, y, z);
PointCoordinates pc = new PointCoordinates(p, line);
words.add(pc);
// just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
}
chopper.close();
} catch (Exception e) {
System.out.println(e);
}
return words;
}
publicArrayList加载文件(字符串文件名){
ArrayList words=新的ArrayList();
试一试{
扫描仪切碎器=新扫描仪(新文件(文件名));
对于(int line=0;chopper.hasNext();line++){
double x=斩波器。nextDouble();
双y=chopper.nextDouble();
双z=斩波器。nextDouble();
点3d p=新点3d(x,y,z);
点坐标pc=新点坐标(p,线);
添加(pc);
//只要调用nextLine,就会在文件末尾出现异常,除非您故意在那里有一个空行,所以这确保了它会出现异常
}
chopper.close();
}捕获(例外e){
系统输出打印ln(e);
}
返回单词;
}
这样,您就可以将这些信息用于您想做的任何事情。您可以在加载文件后执行此操作,如下所示:
Map<Integer, Point3d> positiveList = new java.util.HashMap<Integer, Point3d>();
Map<Integer, Point3d> negativeList = new java.util.HashMap<Integer, Point3d>();
for (int i = 0; i < words.size(); i++) {
Point3d p = words.get(i);
if (p.x < 0 || p.y < 0 || p.z < 0) {
negativeList.put(i + 1, p);
} else {
positiveList.put(i + 1, p);
}
}
Map positiveList=new java.util.HashMap();
Map negativeList=newjava.util.HashMap();
for(int i=0;i
或者将上述内容集成到while循环中,这样可以避免对所有单词进行两次迭代。因此,如果任何一个坐标为负,则该点应位于负列表中
?结果的表达式可能意味着您希望有一个数组作为结果,对吗?因此它只是如果(x<0 | y<0 | z<0)/*添加到否定列表*/;else/*添加到positiveList*/代码>。您在尝试编写此代码时遇到了什么问题?
public ArrayList<Point3d> loadFile(String filename) {
ArrayList<PointContainer> words = new ArrayList<PointContainer>();
try {
Scanner chopper = new Scanner(new File(filename));
for (int line = 0; chopper.hasNext(); line ++) {
double x=chopper.nextDouble();
double y=chopper.nextDouble();
double z=chopper.nextDouble();
Point3d p=new Point3d(x, y, z);
PointCoordinates pc = new PointCoordinates(p, line);
words.add(pc);
// just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
}
chopper.close();
} catch (Exception e) {
System.out.println(e);
}
return words;
}
Map<Integer, Point3d> positiveList = new java.util.HashMap<Integer, Point3d>();
Map<Integer, Point3d> negativeList = new java.util.HashMap<Integer, Point3d>();
for (int i = 0; i < words.size(); i++) {
Point3d p = words.get(i);
if (p.x < 0 || p.y < 0 || p.z < 0) {
negativeList.put(i + 1, p);
} else {
positiveList.put(i + 1, p);
}
}