Modified getDirection() to use arc tangent so that it maps angles smoothly
to their nearest compass direction instead of the wacky way it did before. Also added getFineDirection() along with rotateCW() and rotateCCW() to rotate through (fine) directions (though coarse directions can be rotated as well, simply by always requesting two ticks of rotation). Also wrote some test code to make sure everything works. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1545 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// $Id: DirectionTest.java,v 1.1 2002/06/26 02:54:56 mdb Exp $
|
||||
|
||||
package com.threerings.util;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests the {@link Direction} class.
|
||||
*/
|
||||
public class DirectionTest extends TestCase
|
||||
implements DirectionCodes
|
||||
{
|
||||
public DirectionTest ()
|
||||
{
|
||||
super(DirectionTest.class.getName());
|
||||
}
|
||||
|
||||
public void runTest ()
|
||||
{
|
||||
int orient = NORTH;
|
||||
|
||||
for (int i = 0; i < FINE_DIRECTION_COUNT; i++) {
|
||||
// System.out.print(DirectionUtil.toShortString(orient) + " -> ");
|
||||
orient = DirectionUtil.rotateCW(orient, 1);
|
||||
}
|
||||
// System.out.println(DirectionUtil.toShortString(orient));
|
||||
assertTrue("CW rotate", orient == NORTH);
|
||||
|
||||
for (int i = 0; i < FINE_DIRECTION_COUNT; i++) {
|
||||
// System.out.print(DirectionUtil.toShortString(orient) + " -> ");
|
||||
orient = DirectionUtil.rotateCCW(orient, 1);
|
||||
}
|
||||
// System.out.println(DirectionUtil.toShortString(orient));
|
||||
assertTrue("CCW rotate", orient == NORTH);
|
||||
}
|
||||
|
||||
public static Test suite ()
|
||||
{
|
||||
return new DirectionTest();
|
||||
}
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
DirectionTest test = new DirectionTest();
|
||||
test.runTest();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// $Id: DirectionViz.java,v 1.1 2002/06/26 02:54:56 mdb Exp $
|
||||
|
||||
package com.threerings.util;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
/**
|
||||
* Renders the output of {@link DirectionUtil#getDirection} just for
|
||||
* kicks.
|
||||
*/
|
||||
public class DirectionViz extends JPanel
|
||||
implements MouseMotionListener
|
||||
{
|
||||
public DirectionViz ()
|
||||
{
|
||||
addMouseMotionListener(this);
|
||||
}
|
||||
|
||||
public void doLayout ()
|
||||
{
|
||||
super.doLayout();
|
||||
_center = new Point(getWidth() / 2, getHeight() / 2);
|
||||
}
|
||||
|
||||
public void paintComponent (Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
|
||||
g.setColor(Color.blue);
|
||||
g.drawLine(_center.x, _center.y, _spot.x, _spot.y);
|
||||
|
||||
int orient = DirectionUtil.getFineDirection(_center, _spot);
|
||||
g.drawString(DirectionUtil.toShortString(orient), _spot.x, _spot.y);
|
||||
}
|
||||
|
||||
public void mouseDragged (MouseEvent event)
|
||||
{
|
||||
}
|
||||
|
||||
public void mouseMoved (MouseEvent event)
|
||||
{
|
||||
_spot.x = event.getX();
|
||||
_spot.y = event.getY();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
JFrame frame = new JFrame();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.getContentPane().add(new DirectionViz(), BorderLayout.CENTER);
|
||||
frame.setSize(300, 300);
|
||||
frame.show();
|
||||
}
|
||||
|
||||
protected Point _center;
|
||||
protected Point _spot = new Point();
|
||||
}
|
||||
Reference in New Issue
Block a user