// Ant.java import java.awt.*; class Ant { public Ant () { _x = 0D; _y = 0D; _vx = 0D; _vy = 0D; _turn = 0D; _heading = 0D; _fx = 0D; _fy = 0D; _speed = 3D; _roSpeed = 0.1D; _timer = 0; _sTimer = 0; _target = null; _dx = 0D; _dy = 0D; _dTarget = 0D; _color = Color.red; _mode = DEAD; } public static void SetBounds (double mx, double my) {_maxX = mx; _maxY = my;} public void SetColor (Color c) {_color = c;} public void SetMode (int i) {_mode = i;} public void SetTarget (Ant ant) {_target = ant;} public void SetPosition (double ix, double iy) {_x = ix; _y = iy;} public void Display (Graphics g) { g.setColor (_color); Polygon polygon = new Polygon (); polygon.addPoint ((int)(_x + _vx * 1.6D), (int)(_y + _vy * 1.6D)); polygon.addPoint ((int)(_x + Math.cos(_heading + 5D) * 3D), (int)(_y + Math.sin(_heading + 5D) * 3D)); polygon.addPoint ((int)(_x + Math.cos(_heading - 5D) * 3D), (int)(_y + Math.sin(_heading - 5D) * 3D)); g.fillPolygon (polygon); g.drawLine ((int)_x, (int)_y, (int)(_x + _vx * 2.3D), (int)(_y + _vy * 2.3D)); } public void Move () { _heading += _turn; _vx = Math.cos (_heading) * _speed; _vy = Math.sin (_heading) * _speed; _x += _vx; _y += _vy; _x += _fx; _y += _fy; // wrap if (_x > _maxX) _x -= _maxX; else if (_x < 0D) _x += _maxX; if (_y > _maxY) _y -= _maxY; else if (_y < 0D) _y += _maxY; } public void Think () { switch (_mode) { case SEARCHING: if (--_timer == 0) {_mode = TRACKING; break;} int i = (int)(Math.random() * 100D) - _sTimer; if (--_sTimer == 0) {_turn = 0D; break;} if (i <= 100) break; _turn = ((i & 1) == 1) ? _roSpeed : -_roSpeed; _sTimer = (int)(Math.random() * 10D); break; case TRACKING: _dx = _target._x - _x; _dy = _target._y - _y; double d = _dx + Math.cos(_heading) * _speed; double d2 = _dy + Math.sin(_heading) * _speed; double d4 = d * d + d2 * d2; if (d4 <= 10D) {_turn = 0.0D;} else { double d1 = _dx + Math.cos (_heading + _roSpeed) * _speed; double d3 = _dy + Math.sin (_heading + _roSpeed) * _speed; double d5 = d1 * d1 + d3 * d3; d1 = _dx + Math.cos (_heading - _roSpeed) * _speed; d3 = _dy + Math.sin (_heading - _roSpeed) * _speed; double d6 = d1 * d1 + d3 * d3; if (d4 < 100D && Math.abs ((d6 - d5) / 2D) > 0.01D) {_mode = SEARCHING; _timer = (int)(Math.random() * 50D) + 5;} if (d5 < d6) {_turn = (d5 < d4) ? -_roSpeed : 0D;} else if (d6 < d5) {_turn = (d6 < d4) ? _roSpeed : 0D;} else {_turn = 0.0D;} } _dTarget = d4; break; default: break; } } static double _maxX; static double _maxY; public double _x; public double _y; public double _vx; public double _vy; public double _turn; public double _heading; public double _fx; public double _fy; public double _speed; public double _roSpeed; public int _timer; public int _sTimer; public Ant _target; public double _dx; public double _dy; public double _dTarget; public int _mode; public Color _color; public static final int DEAD = 0; public static final int TRACKING = 1; public static final int SEARCHING = 2; }