Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Get points along a line in Java Swing

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get points along a line in Java Swing

    Iam doing a java swing application. In that i need to get points along the line. Can you please help me with this?
    Please help me


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get points along a line in Java Swing

    I have the following code with me. But sometimes it won't give me correct points

    public static Point[] PointsAlongLine(Point start, Point end, int spacing) {
    int xDifference = end.x - start.x;
    int yDifference = end.y - start.y;
    int absoluteXdifference = Math.abs(start.x - end.x);
    int absoluteYdifference = Math.abs(start.y - end.y);

    int lineLength = (int)Math.sqrt((Math.pow(absoluteXdifference, 2) + Math.pow(absoluteYdifference, 2))); //pythagoras
    int steps = lineLength / spacing;
    int xStep = xDifference / steps;
    int yStep = yDifference / steps;

    Point[] result = new Point[steps+1];

    for (int i = 0; i < steps; i++) {
    int x = start.x + (xStep * i);
    int y = start.y + (yStep * i);
    result[i] = new Point(x, y);
    }
    result[steps]=end;
    return result;
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Get points along a line in Java Swing

    Can you post the results of the program and explain what is wrong with them and show what the results should be?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. I like plotting points :) HI!
    By fractalorbit in forum Member Introductions
    Replies: 1
    Last Post: September 5th, 2011, 10:49 AM
  3. Getting all points in line
    By Mike in forum Java Theory & Questions
    Replies: 7
    Last Post: September 13th, 2010, 11:59 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM