简体   繁体   中英

How to find whether co-ordinate point is negative or not

I have a .txt file with following format data(co-ordinates)

0   1     12.56
2   0     -56.2
1   2     78.2
0  -56.2  2
-2  8     0

I imported this data into using the following code.

  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;
}

importing is working fine.Know I want to separate negative, positive coordinates also I want to append their corresponding index values.

Finally I want result in the following way.

Result :

 positiveList= {{0,0,1,12.56},{2,1,2,78.2}}

 negativeList={{1,2,0,-56.2},{3,0,-56.2,2},{4,-2,8,0}}

How can I do this.

My solution here uses the Map data structure, but you can create 2 Lists of Lists if you wish.

Outside the while loop add:

Map<Integer, Point3D> negativeCoord = new HashMap<>();
Map<Integer, Point3D> positivetiveCoord = new HashMap<>();
int currentIndex = 0;

and inside it:

Point3d p = new Point3d(x, y, z);
if(x < 0 || y < 0 || z < 0) {
    negativeCoord.put(currentIndex, p);
} else {
    positiveCoord.put(currentIndex, p);
}
currentIndex++;

This would be the solution to just have the positives and negatives, without knowing their order.

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.
}

Depending on your scenario it could be wise to have an own container class for the information.

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;
    }
}

and then populate your list with it.

  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;
}

That way you have that information ready to use for everything you want to do.

You could do this after loading the file, like so:

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);
  }
}

or integrate the above in your while-loop, what saves you from iterating over all the words twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM