# Robocode 基本原理之坐标锁定

## 坐标基本概念

All coordinates are expressed as (x,y).

All coordinates are positive.

The origin (0,0) is at the bottom left of the screen.

Positive x is right. X 的右边为正

Positive y is up. Y 的上面为正

## “动静机器人”测试法

``` Geny:
package test;
import robocode.*;
{
public void run ()
{
while (true)
{
// round 对 get 到的数据进行四舍五入处理
out.println("x:"+Math.round(getX()));
out.println("y:"+Math.round(getY()));
}
}
}```

GenyTrack:

``` package test;
import robocode.*;

{
public void run ()
{
while (true)
{
}
}
public void onScannedRobot(ScannedRobotEvent e)
{
double bearing = (getHeading() + e.getBearing()) % 360;
double distance = e.getDistance();

double genyX = getX() + Math.sin(bearing) * distance;
double genyY = getY() + Math.cos(bearing) * distance;

out.println("genyX:"+ Math.round(genyX));
out.println("genyY:"+ Math.round(genyY));
}
}```

## 坐标探测

GenyGenyTrack
X:303genyX:303
Y:128genyY:128

##### 列表 2：
``` double bearing = (getHeading() + e.getBearing()) % 360;
double distance = e.getDistance();

double genyX = getX() + Math.sin(bearing) * distance;
double genyY = getY() + Math.cos(bearing) * distance;

out.println("genyX:"+ Math.round(genyX));
out.println("genyY:"+ Math.round(genyY));```

##### 图 4

``` Sin
public static double sin(double a)
Returns the trigonometric sine of an angle.
Parameters:
a - an angle, in radians.
Returns:
the sine of the argument```

Sin 函数返回三角的正弦函数，参数 a 是一个以 double 类型以弧度表示的角度值，返回类型为 double.

``` cos
public static double cos(double a)
Returns the trigonometric cosine of an angle.
Parameters:
a - an angle, in radians.
Returns:
the cosine of the argument```

Cos 函数返回三角的余弦函数，参数 a 是一个以 double 类型的弧度表示的角值 , 返回类型为 double.

```public void onScannedRobot(ScannedRobotEvent event) {
.getDistance();
.getDistance();
}```

## 移动锁定

##### 列表 3:
``` public void onScannedRobot( ScannedRobotEvent e )
{
double distance = e.getDistance(); // 求得距离
double genyX = getX() + Math.sin(ager_bearing) * distance;
double genyY = getY() + Math.cos(ager_bearing) * distance;
out.println("genyX:"+ Math.round(genyX));
out.println("genyY:"+ Math.round(genyY));

if( 0 <= bearing && bearing <= 180 )
{
}
else if( bearing <= -180 )
{
bearing = ( 360 + bearing );
}
else if( bearing < 0 )
{
bearing =( -bearing );
}
else
{
bearing = (360 - bearing);
}
radar_degree = bearing * 1.3 ; // 加大每一时间周期 (tick) 的扫描范围

{
execute();
}
else
{
execute();
}```

## 三角函数基础

### 2. 直角三角函数

sina= 对边 BC/ 斜边 AB

cosa= 邻边 AC/ 斜边 AB

### 4 ．弧度制

#### 评论

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=10
Zone=Java technology
ArticleID=53119
ArticleTitle=Robocode 基本原理之坐标锁定
publish-date=11072002