📄 util.java
字号:
// Copyright (C) 2002 Takeshi Morimoto <morimoto@takopen.cs.uec.ac.jp>// All rights reserved.// Edited By Omid Aghazadehpackage yab.agent;import java.util.*;import yab.agent.object.*;//saeed 85-2-3public class Util { public static final int distance(RealObject from, RealObject to) { float dx = to.x() - from.x(); float dy = to.y() - from.y(); return (int) Math.sqrt(dx * dx + dy * dy); } public static final int direction(RealObject from, RealObject to) { float dx = to.x() - from.x(); float dy = to.y() - from.y(); float theta = (float) Math.atan2( -dx, dy); if (theta < 0) theta += 2 * Math.PI; return (int) (theta * 360 * 60 * 60 / (2 * Math.PI)); } public static String classBaseName(Object obj) { StringTokenizer st = new StringTokenizer(obj.getClass().getName(), "."); String token = null; while (st.hasMoreTokens()) token = st.nextToken(); return token; } public static int max(int x, int y) { return x >= y ? x : y; } public static int min(int x, int y) { return x <= y ? x : y; } public static Object randomChoice(Collection col, Random random) { if (col.isEmpty())throw new Error("the col must not be empty."); Iterator it = col.iterator(); for (int i = random.nextInt(col.size()); i > 0; i--) it.next(); return it.next(); } // public static final int distance(int x1, int y1, int x2, int y2) { float dx = x1 - x2; float dy = y1 - y2; return (int) Math.sqrt(dx * dx + dy * dy); } public static final int direction(int x1, int y1, int x2, int y2) { float dx = x2 - x1; float dy = y2 - y1; float theta = (float) Math.atan2( -dx, dy); if (theta < 0) theta += 2 * Math.PI; return (int) (theta * 360 * 60 * 60 / (2 * Math.PI)); } public static Comparator idComparator = new Comparator() { public int compare(Object o1, Object o2) { RCRObject r1 = (RCRObject) o1; RCRObject r2 = (RCRObject) o2; if (r1.id > r2.id) return 1; if (r1.id == r2.id) return 0; return -1; } }; public static boolean Position(double x0, double y0, double ang, double tg, Wall edg) { double a = edg.x1; double b = edg.y1; double c = edg.x2; double d = edg.y2; if (ang == 0f || ang == Math.PI) { if (isBetween(a, x0, c)) { if (a == c) { edg.distance = Util.distance((int) x0, (int) y0, (int) a, (int) b); if (a > x0) { edg.right = true; } else { edg.right = false; } } else { edg.distance = Util.distance((int) x0, (int) y0, (int) x0,(int) ((b - d) * (x0 - c) / (a - c) +d)); edg.right = false; } return true; } edg.distance = 0; return false; } else { if (a == c) { double y = tg * (a - x0) + y0; if (isBetween(b, y, d)) { edg.distance = Util.distance((int) x0, (int) y0, (int) a,(int) y); if (a > x0) { edg.right = true; } else { edg.right = false; } return true; } edg.distance = 0; return false; } if (a * tg - c * tg + d - b == 0) { edg.distance = 0; return false; } double x = ( -b * c + d * a + c * y0 + a * tg * x0 - a * y0 - c * tg * x0)/ (a * tg - c * tg + d - b); if (isBetween(a, x, c)) { if (a == c) { edg.distance = Util.distance((int) x0, (int) y0, (int) a,(int) b); if (a > x0) { edg.right = true; } else { edg.right = false; } } else { edg.distance = Util.distance((int) x0, (int) y0, (int) x,(int) ((b - d) * (x - c) / (a - c) + d)); if (x > x0) { edg.right = true; } else { edg.right = false; } } return true; } edg.distance = 0; return false; } } public static boolean isBetween(double a, double x, double b) { if (a > b) { double k = a; a = b; b = k; } if (x >= a && x <= b) { return true; } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -