Iterate along a line point by point

Hey,
Is there a way to follow a Line2D point by point? If the line is declared like this:
Point p1 = new Point(200, 20);
Point p2 = new Point(200, 40);
Line2D.Double line = new Line2D.Double(p1, p2);then it's easy to know what the next step on the line is, since the line is vertical. All I'll have to do is to start at one point (e.g. p1) and iterate 20 steps on the y-axis from 20 to 40, always knowing that I'm on the line.
But how can I iterate along a line point by point, if the line is declared like this:
Point p1 = new Point(130, 47);
Point p2 = new Point(200, 89);
Line2D.Double line = new Line2D.Double(p1, p2);Thanks in advance!

But how can I iterate along a line point by point, if
the line is declared like this:
Point p1 = new Point(130, 47);
Point p2 = new Point(200, 89);
Line2D.Double line = new Line2D.Double(p1, p2);Thanks in advance!Let's not get too complicated. Just write an equation for an x and y intercept and decide by how much do you want to iterate.

Similar Messages

  • Problem!!! Moving point along the line

    hi,
    I need to move the point along the line. I want move this point using mouse.
    My program draws a line and a point that' s on this line.
    Could someone help me.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    public class TraverseAgain extends JPanel {
        Point2D.Double[] points;
        Point2D.Double p1;
        Point2D.Double p2;
        /** An endPoint of one of the lines is being moved. */
        public void setEndPoint(int index, double x, double y) {
            int start;
            double ratio;
            switch(index) {
                case 0:
                    start = index == 0 ? 1 : 0;
                    ratio = getRatio(p1, start, index);
                    resetMidPoint(p1, points[start], x, y, ratio);
                    break;
                case 1:
                    start = index == 0 ? 1 : 0;
                    ratio = getRatio(p1, start, index);
                    resetMidPoint(p1, points[start], x, y, ratio);
                    start = index == 1 ? 2 : 1;
                    ratio = getRatio(p2, start, index);
                    resetMidPoint(p2, points[start], x, y, ratio);
                    break;
                case 2:
                    start = index == 1 ? 2 : 1;
                    ratio = getRatio(p2, start, index);
                    resetMidPoint(p2, points[start], x, y, ratio);
                    break;
                default:
                    System.out.println("illegal index: " + index);
            points[index].setLocation(x, y);
            repaint();
         * Design assumption/decision:
         * Preserve this ratio when repositioning a midPoint between the
         * end points of its line when one of the end points is being moved.
        private double getRatio(Point2D.Double p, int start, int end) {
            double d1 = points[start].distance(p);
            double d2 = points[start].distance(points[end]);
            return d1 / d2;
        private void resetMidPoint(Point2D.Double p, Point2D.Double end,
                                   double x, double y, double ratio) {
            double dy = y - end.y;
            double dx = x - end.x;
            double theta = Math.atan2(dy, dx);
            double length = end.distance(x, y);
            x = end.x + length*ratio*Math.cos(theta);
            y = end.y + length*ratio*Math.sin(theta);
            p.setLocation(x, y);
        /** A midPoint is being moved. */
        public void setMidPoint(int number, double x, double y) {
            switch(number) {
                case 1:
                    double dx = points[1].x - points[0].x;
                    double dy = points[1].y - points[0].y;
                    if(points[0].x < x && x < points[1].x ||
                       points[1].x < x && x < points[0].x)
                        y = points[0].y + (x - points[0].x)*(dy / dx);
                    else if(points[0].y < y && y < points[1].y ||
                            points[1].y < y && y < points[0].y)
                        x = points[0].x + (y - points[0].y)*(dx / dy);
                    else
                        return;
                    p1.setLocation(x, y);
                    break;
                case 2:
                    dx = points[2].x - points[1].x;
                    dy = points[2].y - points[1].y;
                    if(points[1].x < x && x < points[2].x ||
                       points[2].x < x && x < points[1].x)
                        y = points[1].y + (x - points[1].x)*(dy / dx);
                    else if(points[1].y < y && y < points[2].y ||
                            points[2].y < y && y < points[1].y)
                        x = points[1].x + (y - points[1].y)*(dx / dy);
                    else
                        return;
                    p2.setLocation(x, y);
                    break;
                default:
                    System.out.println("illegal number " + number);
            repaint();
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(points == null)
                initGeoms();
            g2.setPaint(Color.blue);
            for(int j = 1; j < points.length; j++)
                g2.draw(new Line2D.Double(points[j-1].x, points[j-1].y,
                                          points[j].x,   points[j].y));
            g2.setPaint(Color.red);
            for(int j = 0; j < points.length; j++)
                mark(points[j], g2);
            g2.setPaint(Color.green.darker());
            mark(p1, g2);
            mark(p2, g2);
        private void mark(Point2D.Double p, Graphics2D g2) {
            g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
        private void initGeoms() {
            int w = getWidth();
            int h = getHeight();
            points = new Point2D.Double[3];
            points[0] = new Point2D.Double(w/4, h/3);
            points[1] = new Point2D.Double(w/16, h/2);
            points[2] = new Point2D.Double(w*13/15, h*3/4);
            double cx = points[0].x + (points[1].x - points[0].x)/2;
            double cy = points[0].y + (points[1].y - points[0].y)/2;
            p1 = new Point2D.Double(cx, cy);
            cx = points[1].x + (points[2].x - points[1].x)/2;
            cy = points[1].y + (points[2].y - points[1].y)/2;
            p2 = new Point2D.Double(cx, cy);
        public static void main(String[] args) {
            TraverseAgain test = new TraverseAgain();
            PointMover mover = new PointMover(test);
            test.addMouseListener(mover);
            test.addMouseMotionListener(mover);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    class PointMover extends MouseInputAdapter {
        TraverseAgain traverseAgain;
        int selectedEndIndex = -1;
        int selectedMidNumber = -1;
        Point2D.Double offset = new Point2D.Double();
        boolean dragging = false;
        final int MIN_DIST = 5;
        public PointMover(TraverseAgain ta) {
            traverseAgain = ta;
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            Point2D.Double[] points = traverseAgain.points;
            for(int j = 0; j < points.length; j++) {
                if(points[j].distance(p) < MIN_DIST) {
                    selectedEndIndex = j;
                    rigForDrag(points[j], p);
                    break;
            if(selectedEndIndex == -1) {
                if(traverseAgain.p1.distance(p) < MIN_DIST) {
                    selectedMidNumber = 1;
                    rigForDrag(traverseAgain.p1, p);
                } else if(traverseAgain.p2.distance(p) < MIN_DIST) {
                    selectedMidNumber = 2;
                    rigForDrag(traverseAgain.p2, p);
        private void rigForDrag(Point2D.Double p, Point loc) {
            offset.x = loc.x - p.x;
            offset.y = loc.y - p.y;
            dragging = true;
        public void mouseReleased(MouseEvent e) {
            dragging = false;
            selectedEndIndex = -1;
            selectedMidNumber = -1;
        public void mouseDragged(MouseEvent e) {
            if(dragging) {
                double x = e.getX() - offset.x;
                double y = e.getY() - offset.y;
                if(selectedEndIndex != -1)
                    traverseAgain.setEndPoint(selectedEndIndex, x, y);
                else
                    traverseAgain.setMidPoint(selectedMidNumber, x, y);
    }

  • 2D Graphics (I can't print thin line, point, etc.)

    I created a program that draw and print
    a graph.
    I have used 2D Graphics.
    But I have some problem:
    I can't print thin line, point, etc.
    Thanks in advance
    Gali
    null

    I created a program that draw and print
    a graph.
    I have used 2D Graphics.
    But I have some problem:
    I can't print thin line, point, etc.
    Thanks in advance
    Gali
    null

  • I have just redeemed my iTunes card of 25 dollars, I have 23 dolIars left now, I tried downloading a few songs later, but it says something along the lines of credit mismatch mobile message, I can't download anything with credit. How do I fix the problem?

    I really need help, I purchased an iTunes card about an hour ago and downloaded two things, I tried to download a couple songs but it won't let me, every time I try it says something along the lines of credit mismatch mobile message. How do I fix this? It says I have 23 dollars left but it's not working. How do I fix the problem? Please help!

    its very annoying but you have to log out of your account. than try to buy the sone. it will ask you to sign in. do it. the sond will start to download. it only works once, so you have to do it over and over for every song :/

  • ITunes unable to recognize iPod, error message along the lines if "iTunes has detected an iPod but unable to recognise" I have followed all the trouble shooting tips and am still stuck? I am now being told by a friend I need a link sent to me to fix this

    Hi,
    I am need of help! I have had this issue for a while now and it's becoming frustrating.
    My iTunes installed on my HP Windows 8 Netbook is unable to recognize my iPod, the error message along the lines of "iTunes has detected an iPod but unable to recognise"
    I have followed all the trouble shooting tips and am still stuck? I am now being told by a friend I need a link sent to me to fix this??
    Someone please help me fix this problem.
    Many thanks.
    Matt.

    Hi
    So just close iTunes and re-open iTunes thats it and that worked for you?
    When you say reset your settings do you mean to factory settings? As my iPod was swapped at the store for a new one and the problem is still there...
    Thanks.

  • I bought my I pod touch 3-4 months ago and I got this message all of a sudden, it said something along the lines of provisonal complete_Distrib... will expire in 30 days. What is this it's under setting-general-Profile should I be worried?

    I bought my I pod touch 3-4 months ago and I got this message all of a sudden, it said something along the lines of provisonal complete_Distrib... will expire in 30 days. What is this it's under setting-general-Profile should I be worried? Should I just let it expire.

    Unless you have a company or school iPod, that profile was installed by an app you installed. It is likely that the app will no longer function when that date is reached. You can Google the name of the profile and that may provide some more information.

  • Opening Balance to be displayed along with line items

    Dear Experts,
    Is there any way to display Opening Balance for a particular GL along with LIne Items. FS10N only provides Period wise opening balance and line items. If I have to select the line items for a GL in the middle of the period and see the opening balance, how is it possible. In FBL3N i am able to see only line items and no opening balance.
    Any suggestions.......................
    Raj.

    Hello,
    FS10N you will see the balance and then double click on that to see line item. Again at line items you will not see the opening balance. There is no such facility.
    If you want period wise balances, then go with
    S_ALR_87012172 report.
    Regards,
    Ravi

  • Iphone 4s stuck during OS upgrade 1/2 way along the line under the Apple LOGO.  what can i do to unstick it.

    during an upgrade to the latest OS version on our IPHONE 4S, it stopped updating about 1/2 way along the line under the APPLE LOGO.  what can I do to complete, or restart the update?

    Hi ..
    Depends on whether you're doing this wirelessley or using iTunes >  Update your iPhone, iPad, or iPod touch
    iOS: Unable to update or restore

  • Bought an album off iTunes and two songs from the album will not download. Is there anyway to make them force load or anything along those lines?

    Bought an album off iTunes and two songs from the album will not download. Is there anyway to make them force load or anything along those lines? The Album is Sunshower-EP by UME and the songs are The Conductor and The Means.

    Hey ChandlerAdaway!
    I have an article here that can provide you some guidance for this issue. First, you will want to check for the songs in your past purchases list for your account, if you haven't already:
    Downloading past purchases from the iTunes Store, App Store, and iBooks Store
    http://support.apple.com/kb/HT2519
    If the songs cannot be found there, you will want to report an issue with your download by following the directions in this article:
    How to report an issue with your iTunes Store, App Store, Mac App Store, or iBooks Store purchase
    http://support.apple.com/kb/HT1933
    Thanks for coming to the Apple Support Communities!
    Regards,
    Braden

  • I think my computer has a virus, when I get on the Internet, ads will pop up all the time, I think it is called adware?  Also pages will pop up saying something along the lines of my computer is at risk for some security issue. What should I do?

    I think my computer has a virus, when I get on the Internet, ads will pop up all the time, I think it is called adware?  Also pages will pop up saying something along the lines of my computer is at risk for some security issue. What should I do?

    Remove Browser Pop-up Problems
         AdwareMedic
         Adblock Plus 1.8.9
         remove adware that displays pop-up ads and graphics on your Mac
    Helpful Links Regarding Malware Problems
    If you are having an immediate problem with ads popping up see The Safe Mac » Adware Removal Guide and AdwareMedic. If you require anti-virus protection Thomas Reed recommends using ClamXAV. (Thank you to Thomas Reed for this recommendation.)
    Open Safari, select Preferences from the Safari menu. Click on Extensions icon in the toolbar. Disable all Extensions. If this stops your problem, then re-enable them one by one until the problem returns. Now remove that extension as it is causing the problem.
    The following comes from user stevejobsfan0123. I have made minor changes to adapt to this presentation.
    Fix Some Browser Pop-ups That Take Over Safari.
    Common pop-ups include a message saying the government has seized your computer and you must pay to have it released (often called "Moneypak"), or a phony message saying that your computer has been infected, and you need to call a tech support number (sometimes claiming to be Apple) to get it resolved. First, understand that these pop-ups are not caused by a virus and your computer has not been affected. This "hijack" is limited to your web browser. Also understand that these messages are scams, so do not pay any money, call the listed number, or provide any personal information. This article will outline the solution to dismiss the pop-up.
    Quit Safari
    Usually, these pop-ups will not go away by either clicking "OK" or "Cancel." Furthermore, several menus in the menu bar may become disabled and show in gray, including the option to quit Safari. You will likely have to force quit Safari. To do this, press Command + option + esc, select Safari, and press Force Quit.
    Relaunch Safari
    If you relaunch Safari, the page will reopen. To prevent this from happening, hold down the 'Shift' key while opening Safari. This will prevent windows from the last time Safari was running from reopening.
    This will not work in all cases. The shift key must be held at the right time, and in some cases, even if done correctly, the window reappears. In these circumstances, after force quitting Safari, turn off Wi-Fi or disconnect Ethernet, depending on how you connect to the Internet. Then relaunch Safari normally. It will try to reload the malicious webpage, but without a connection, it won't be able to. Navigate away from that page by entering a different URL, i.e. www.apple.com, and trying to load it. Now you can reconnect to the Internet, and the page you entered will appear rather than the malicious one.

  • Exception number line pointing to commented code

    Hi
    Im facing a strange problem. In travel management project application, on click of one of the buttons, it throws a null pointer exception.
    In the exception details, it gives me a .java file with the name of the method and line number.
    when I go to that .java file in webdynpro and find the method and the line number, it is actually a commented code.
    Whats wrong? how can I find where exactly the problem is?
    thanks
    manoj

    Hi Sumit
    I have deployed it a couple of times, but the problem persists.
    Any thoughts, suggestions?
    Thanks
    Manoj

  • Moving an image along a line

    hi,
    for a part of a project i need to move an image
    (of a robot) across a path.
    the path is a vector containing line (with a start and ending point).
    i know how to show the image in a point. but
    i need the code to move it along the line

    Look at the Line2D class, it my help.
    Also read this link
    http://forum.java.sun.com/thread.jsp?forum=20&thread=28813

  • Two problems : areaChart along with Line and double y-axis in LineChart

    Hi Friends,
    This forum is very useful as we are getting lots of help from people who are trying out this new technology and giving/using help to/from others.I am developing a real time application in JavaFx in which I need to implement Charts using dynamic data coming from a server.
    Now I am facing two types of problem which may be interrelated in some way.
    Problem 1 :
    I need to combine an areachart along with a line Chart.There is nothing in JavaFx like area-line Chart so should i go with all area charts and make the fill-color of area series transparent(is it possible to have transparent fill in area-chart ? by css ?) OR i should stack two charts one area and one line on top of one another making the background of either transparent(once again is it possible to have transparent background for a chart so that background chart/series is visible ?) OR any other suggestion ? Anyways I tried stacking using stackPane and it turned out to be ugly combination of misaligned charts :-(
    Problem 2 :
    I need to put two y-axis(i.e secondary axis) on the same chart but i am unable to find such feature with my best efforts.I know I can set the side of Axis using setSide method of Axis class but how can i put it both side simultaneously.Also is there a way to put different scales on these axis ? I mean is it possible to have two different series with drastic difference in bounds(data range) to be put on same chart by attaching them to different Y-axis on the same chart ?

    I don't think there is anything in the JavaFX library which matches exactly what you need.
    You might be able to use some tips from the 3d pie chart I created at: https://gist.github.com/1485144. This demonstrates stacking multiple charts on top of each other, whilst removing rendundant details so it doesn't end up a jumbled mess and looking up items by their css tags and modifying them.
    There is now excellent documentation on chart css at http://docs.oracle.com/javafx/2.0/charts/css-styles.htm#CIHGIAGE. If this doesn't get you all of the css hooks you need to dig out the details required from the chart, then you can unpack jfxrt.jar and search for caspian.css or you can recursively print the nodes in the chart which will tell you the type and style of each node. Armed with this information you could should then be able to stack the two charts you created on top of each other, align them correctly and strip away the info you don't need. To get the second y axis to the other side of the chart you could look it up by css in code, then do a translateX on it to so that it is translated by a bind to the width of the x axis. Seems doable, if a little fiddly.
    You might be tempted to directly create an Axis yourself by creating an instance of http://docs.oracle.com/javafx/2.0/api/javafx/scene/chart/NumberAxis.html and laying it over your chart, but I was unable to get that to work (http://javafx-jira.kenai.com/browse/RT-18270) - even if you did so, you would have to manually set up the ranges and ticks as the chart library wouldn't handle that for you. Another way to create a Axis like thing is by creating a http://docs.oracle.com/javafx/2.0/api/javafx/scene/control/Slider.html, disabling user input on it and removing it's thumb via css.
    My efforts at answering chart questions in the past sometimes ended up as "That's not what I need": Re: How to create PieChart like this?
    Questions about charts are hard to answer without a link to an exact image of what the required chart is expected to look like.

  • Along the lines of image resizing

    was having similar issues with "grouped" graphics (lines, rectangle, circles) created in Keynote. I tried to resize the collection and it went " winkey" (techo-speak for 'didn't respond as expected'). I tried toggling the constrained proportions options to no effect, I also noticed, when I went to key in a size, instead of filling the height/ width box, the keyed enter would end up on the screen like a text box. ? ? ?
    The old ms Windows trick of exiting the application a restating seemed to "fix" the key enter problem for a little while.
    Any ideas comments, remedies?
    Mac Pro intel Mac OS X (10.5.6) iworks 09 (demo)

    Simmilar to whom, MM Holden?
    This thread will give you some background: Resizing
    Other than that use guides and smaller groups to help with alignments. Until you provide specifics hard to help more than that.

  • Hi, I am having trouble with my Internet on my Macbook. Every time I try and go to a site it has a pop up block that says something along the lines of "Safari can't verify the identity of the website."

    Below that there is more, it says," The certificate for this website is invalid. You might be connecting to a website that is pretending to be (Whatever the name of the site is) which could put your confidential info at risk. Would you like to connect to the website anyway?" and it give the options of show certificate, cancel, and continue. But the thing i dont understand is that it does this for basically EVERY page i visit. even facebook. and when i click "continue" it alows me to go to the page but limits what i can do and only shows certain things. i'm very frusterated with it. Help Me!

    This issue was resolved! I didnt think about restoring my computer to factory settings but I did and now my computer works like brand new. basically, just back up your files and do the following:
    http://www.youtube.com/watch?v=yPmT5Xbb8p0

Maybe you are looking for

  • Need advice on Unity Voice Mail Please

    Chaps. Around 4 years ago, i inherited a CCM v4 as part of a business relocation. I've had no formal training on CCM, so everything i know, I've taught myself. The system includes a Unity VM server, which until today has been switched off, as we've h

  • Power Mac G5 DP1.8GHz - Bad Logic Board or Power Management Issue?

    I have a Power Mac G5 DP1.8GHz/1.5GB/80GB which I bought non-working. It has not yet been disassembled or examined by a certified tech. This is it's issue (which replicates): the computer powers on. It makes a single warning tone, then the LED flashe

  • Help!!! My Illustrator PS misinterpreted in Esko's Barco

    My question is specifically for print. Havent encountered something like this in video or Multimedia. For print I work in Adobe Illustrator CS4, Freehand MX and Corel. I generate PS files for final output len files which are processed on Esko's Barco

  • 3g and wireless together, problems

    I have the iphone 4 and when my wireless is connected at home.  My AIM is slow and I usually have to wait a long time before the outgoing emails are sent. The go but can take several minutes Can anyone help with this?

  • Problem about auto backup of composing mail

    I am using Leopard Mail with Gmail IMAP setting When composing mail, Mail automatically makes backup of the mail about every 30 seconds. Therefore I have to delete more than thirty useless "backups" after I sent the mail. Any similar case here? How c