Results 1 to 5 of 5
  1. #1
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Java Topology suit (JTS) - calculating point

    Hi all
    First of; sorry if I'm starting this thread in the wrong section.

    I've created a line with 7 points.
    Then I shall use these coordinates to make another line, but where each linesegment can't be longer than 5 units. If a linesegment is longer than 5, a new point should be added at the point.
    I'm having trouble to calculate where these points should be located (well, not by hand, but when writing a code that does it).

    These points should then be added together with the original coordinates in a new coordinate array to create the new line.

    Would really appreciate comments and tips on how to solve this.

    Java Code:
    import com.vividsolutions.jts.geom.*;
    public class NewProj {
    	public static void main(String[] args){
    	    // Create the original linestring
    	    Coordinate[] coordinatesToLine = new Coordinate[] {
    		new Coordinate(12, 2),
    		new Coordinate(12, 13),
    		new Coordinate(12, 19), 
    		new Coordinate(19, 19), 
    		new Coordinate(20, 20),
    		new Coordinate(25, 25),
    		new Coordinate(27, 18)
    	    };
    	   
    	    
    	    LineString FirstLine = new GeometryFactory().createLineString(coordinatesToLine);
    	    Point pointOne, pointTwo;
    	    double dist;
    	    int newLineLength = FirstLine.getNumPoints();//that number will be changed later
    	    double newCoorX, newCoorY;
    	    Coordinate newCoor;//  'newCoor = new Coordinate(newCoorX, newCoorY);' should be used in the second loop
    	    int points2Add;
    	    
    	    // Loop that parses the line and reads the number of points available.
    	    // it will output an integer values that gives the length of the 
    	    // new line by calculating the distance between each two points.
            for (int counter=1; counter <= FirstLine.getNumPoints()-1;counter++){
    		pointOne = FirstLine.getPointN(counter-1);
    		pointTwo = FirstLine.getPointN(counter);
    		dist = pointOne.distance(pointTwo);
    		if ( dist > 5 ){
    		    newLineLength = newLineLength + (int)(dist / 5);//here new number of points are added
    		}
    	    }
            
            
            //  Here is the section where the program should calculate the new points...
    	    // I think this can be done by a loop similar to the one above. then I shall put the coordinates
    	    //   in the 'newCoordinates'-array.
            // I know that this section probably will need to be re-written completely. It's just a try...
             	   for (int counter=1; counter <= newLineLength;counter++){
             		   pointOne = FirstLine.getPointN(counter-1);
             		   pointTwo = FirstLine.getPointN(counter);
             		   
             		   Coordinate[] newCoordinates = new Coordinate[newLineLength];
             		   pointOne.getX();pointOne.getY();
             		   pointTwo.getX();pointTwo.getY();
             		   
             		   Geometry SecondLine = new GeometryFactory().createLineString(newCoordinates);
             		   if ( pointOne.getX() - pointTwo.getX() > 5 ){
             			  newCoorX =  pointOne.getX() +5; 
             		  }
             		   if ( pointOne.getY() - pointTwo.getY() > 5 ){
              			  newCoorY =  pointOne.getY() +5; 
              		   }
            System.out.println("Coordinates of SecondLine: " + SecondLine.toString());
             	    
    
             	   }
    	}
             	   
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    can't be longer than 5 units
    As in, the distance between two points? You can code a version of the distance between two points formula to check the length. In the event that the length is too long, you can use some trig to find the point on the line where a new vertex needs to be added.

    The intersection of a line with a circle comes to mind (where the radius is 5 units, and the line has an angle from the origin to some destination point beyond the circle).

  3. #3
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    As in, the distance between two points?
    Yes, the distance between each (two) points can't be longer than five units.

    The intersection of a line with a circle comes to mind (where the radius is 5 units, and the line has an angle from the origin to some destination point beyond the circle).
    Any tips on how to implement this - in code?

    Thank's for your reply.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Any tips on how to implement this - in code?
    Well, before we get to that, do you know how to do it on paper?

  5. #5
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. calculating taxes in java
    By KAM0002 in forum New To Java
    Replies: 16
    Last Post: 12-08-2010, 12:41 PM
  2. how i can do this in java main and class point
    By ssssami in forum New To Java
    Replies: 6
    Last Post: 09-24-2010, 02:55 AM
  3. java floating point comparison
    By sardare in forum Advanced Java
    Replies: 6
    Last Post: 03-03-2009, 05:11 PM
  4. [SOLVED] Calculating Std. Deviation using Java
    By random0munky in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 10:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •