java球员_java(三) - 关于机器人足球之根据球场上已知信息求球员位置

机器人技术第三次作业.代码说明

题目

7bb10ee1a8e0c08a840de51fdfdc6688.png

测试平台

编程语言: java - JDK13

操作系统: windows10 Enterprise

实验结果

球场根据已知信息求交点

测试代码

public static void main(String[] args) {

Message message = new Message("P8", 22.f, 0.f, "P7", 27.7128129f, 30);

Player player = new Player(message);

// 输出测试验证

System.out.println("------------MESSAGE-------------");

System.out.println(message.toString());

System.out.println("----------INTERSECTION----------");

System.out.println(player.toString());

}

下图为运行结果

2206c42c5fae1a5538f1a9fe993b3fad.png

实验代码

package robot;

import java.util.Arrays;

public class week3 {

public static void main(String[] args) {

Message message = new Message("P8", 22.f, 0.f, "P7", 27.7128129f, 30);

Player player = new Player(message);

System.out.println("------------MESSAGE-------------");

System.out.println(message.toString());

System.out.println("----------INTERSECTION----------");

System.out.println(player.toString());

}

}

// 点类

class Point {

private float x;

private float y;

Point(float x, float y) {

this.x = x;

this.y = y;

}

Point(Point point) {

this.x = point.getX();

this.y = point.getY();

}

public float getX() {

return x;

}

public float getY() {

return y;

}

public void setX(float x) {

this.x = x;

}

public void setY(float y) {

this.y = y;

}

public float getDist(Point point) {

return (float) Math.sqrt(Math.pow( (this.x - point.getX()), 2) + Math.pow((this.y - point.getY()), 2) );

}

@Override

public String toString() {

return "Point{ ( " + x + ", " + y + " ) }";

}

}

// 球场类

class PlayField {

// C点

static final private Point pointC = new Point(0.f, 0.f);

// P点集合

static final private Point[] pointsP = {

new Point(-52.5f, -32.f),

new Point(-52.5f, 32.f),

new Point(52.5f, 32.f),

new Point(52.5f, -32.f),

new Point(0.f, -32.f),

new Point(0.f, 32.f),

new Point(-30.f, -7.f),

new Point(-30.f, 7.f),

new Point(30.f, 7.f),

new Point(30.f, -7.f)};

// G点集合

static final private Point[] pointsG = {

new Point(-52.5f, 0.f),

new Point(52.5f, 0.f)};

public static Point getPos(String point) {

if (point.length() == 2) {

if (point.charAt(0) == 'P') {

return pointsP[Integer.parseInt(String.valueOf(point.charAt(1))) - 1];

}

else if (point.charAt(0) == 'G') {

return pointsG[Integer.parseInt(String.valueOf(point.charAt(1))) - 1];

}

else if (point.charAt(0) == 'C') {

return pointC;

}

}

// string 不能表示球场上点的名字

return null;

}

public static Point getPointC() {

return pointC;

}

public static Point[] getPointsG() {

return pointsG;

}

public static Point[] getPointsP() {

return pointsP;

}

}

// 圆类

class Circle {

private Point center; // 圆心

private float r; // 半径

Circle(float x, float y, float r) {

this.center = new Point(x, y);

this.r = r;

}

Circle(Point center, float r) {

this.center = center;

this.r = r;

}

public float getR() {

return r;

}

public Point getCenter() {

return center;

}

}

// 信息类 - 随机得到附近 2点距离自己的信息

class Message {

private Point[] pts = new Point[2];

private float[] rs = new float[2];

private float[] thetas = new float[2];

Message(String pt0, float r0, float theta0, String pt1, float r1, float theta1) {

pts[0] = PlayField.getPos(pt0);

pts[1] = PlayField.getPos(pt1);

rs[0] = r0;

rs[1] = r1;

thetas[0] = theta0;

thetas[1] = theta1;

}

public Point getPts(int index) {

return pts[index];

}

public float getRs(int index) {

return rs[index];

}

public float getThetas(int index) {

return thetas[index];

}

@Override

public String toString() {

return "Message{ Point0: ( ( " + pts[0] + " ), " + rs[0] + ", " + thetas[0] + " );\n"

+ " Point1: ( ( " + pts[1] + " ), " + rs[1] + ", " + thetas[1] + " ) }";

}

}

class Player {

Point position;

Player(float x, float y) {

position = new Point(x, y);

}

Player(Point point) {

position = new Point(point.getX(), point.getY());

}

Player(Message message) {

float y0 = message.getPts(0).getY();

float y1 = message.getPts(1).getY();

// y值大的作为第二个point

Point point0 = null;

Point point1 = null;

float theta0 = 0.f;

float theta1 = 0.f;

float r0 = 0.f;

float r1 = 0.f;

// 将y坐标较大的作为point1

if (y0 > y1) {

point0 = message.getPts(1);

point1 = message.getPts(0);

r0 = message.getRs(1);

r1 = message.getRs(0);

theta0 = message.getThetas(1);

theta1 = message.getThetas(0);

}

else {

point0 = message.getPts(0);

point1 = message.getPts(1);

r0 = message.getRs(0);

r1 = message.getRs(1);

theta0 = message.getThetas(0);

theta1 = message.getThetas(1);

}

// c 为球员到两个相关点的夹角 ( 0

float c = theta1 - theta0;

int flag = 0;

// 用 flag 来标识 c 取值四种情况

// 分别对应如下, 在之后分别对四种情况进行处理

// 0 - ∈[-360, -180] 角 c 指向 x>0 的方向 ; 球员面向 c 的补角

// 1 - ∈[-180, 0 ] 角 c 指向 x<0 的方向 ; 球员面向 c 的内角

// 2 - ∈[ 0, 180] 角 c 指向 x>0 的方向 ; 球员面向 c 的补角

// 3 - ∈[ 180, 360] 角 c 指向 x<0 的方向 ; 球员面向 c 的内角

if (c < -180.f && c > -360.f) {

c += 360.f;

}

else if (c < 0.f && c > -180.f) {

c = -c;

flag = 1;

}

else if (c > 0.f && c < 180.f) {

flag = 2;

}

else if (c > 180.f && c < 360.f) {

c = 360 - c;

flag = 3;

}

float dist = point0.getDist(point1);

if (point0.getX() - point1.getX() < 0.05f) {

// 两点连线与y轴平行.

float betaCos = (r0*r0 + dist*dist - r1*r1) / (2*r0*dist);

float beta = (float) Math.acos(betaCos); // ∈[0, PI]

float x = 0.f;

float y = 0.f;

if (flag == 1 || flag == 3) {

x = (float) (point0.getX() + r0 * Math.sin(beta));

y = (point0.getY() + r0 *betaCos);

}

else {

x = (float) (point0.getX() - r0 * Math.sin(beta));

y = (point0.getY() + r0 *betaCos);

}

position = new Point(x, y);

}

else {

// 两点连线与y轴不平行,此时直线斜率存在.

float betaCos = (r0*r0 + dist*dist - r1*r1) / (2*r0*dist);

float beta = (float) Math.acos(betaCos); // ∈[0, PI]

// 直线与 x 轴所成角度为 theta .

float k = (point1.getY() - point0.getY()) / (point1.getX() - point0.getX());

float theta = (float) Math.atan(k);

float gamma = 0.f;

if (flag == 1 || flag == 2) {

gamma = theta - beta;

}

else {

gamma = theta + beta;

}

float x = 0.f;

float y = 0.f;

if (flag == 1 || flag == 3) {

x = (float) (point0.getX() + r0 * Math.sin(gamma));

y = (float) (point0.getY() + r0 * Math.cos(gamma));

}

else {

x = (float) (point0.getX() - r0 * Math.sin(gamma));

y = (float) (point0.getY() + r0 * Math.cos(gamma));

}

position = new Point(x, y);

}

}

@Override

public String toString() {

return "Player{" +

"position=" + position +

'}';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值