Page 269 - The Definitive Guide to Building Java Robots
P. 269

Preston_5564C07.fm  Page 250  Monday, September 26, 2005  5:38 AM



                 250    CHAPTER 7  ■  NAVIGATION



                                } else {
                                    if (a.x < b.x) {
                                        d = 90 - d;
                                    } else {
                                        d = 180 + (90 - d);
                                    }
                                }
                                return new DistanceVector(d, mag);
                            }
                            Next, I want to create a data structure with a name I could store and recall at a later time.
                        I can’t do this with a Point, but I can reuse a Point by just extending it and giving my new class
                        a variable called name. I called this class NavPoint and added the string name to its
                        constructor. See Example 7-12.


                        Example 7-12. NavPoint.java
                        package com.scottpreston.javarobot.chapter7;

                        import java.awt.Point;

                        public class NavPoint extends Point {

                            public static final String START_POINT = "start";
                            public static final String EXIT_POINT = "exit";
                            public static final String CENTER_POINT = "center";

                            public String name = null;


                            public NavPoint(String name) {
                                super();
                                this.name = name;
                            }

                            public NavPoint(String name, int x, int y) {
                                super(x, y);
                                this.name = name;
                            }
                            public NavPoint(String name, Point p) {
                                super(p);
                                this.name = name;
                            }
                        }

                            Next, it’s time for the robot to figure out its start position on its own.
   264   265   266   267   268   269   270   271   272   273   274