当前位置: 编程技术>移动开发
本页文章导读:
▪游戏中判断一群点能否构成凸多边形 游戏中判断一群点可否构成凸多边形
下面的代码需要box2d!!!!!!!!!!!import org.jbox2d.common.vec2;public class polyonutil {<br> private static polyonutil instance;<br> private float a;&l.........
▪ Java 推算日期(计算距今多少年,几多月,多少天的日期) Java 推算日期(计算距今多少年,多少月,多少天的日期)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public final class Utils
{
/**
* get any time which you want before or af.........
▪ 取ARGB色彩的分度值 取ARGB颜色的分度值
private static int getR32(int c) { return (c >> 0) & 0xFF; }
// access the red component from a premultiplied color
private static int getG32(int c) { return (c >> 8) & 0xFF; }
.........
[1]游戏中判断一群点能否构成凸多边形
来源: 互联网 发布时间: 2014-02-18
游戏中判断一群点可否构成凸多边形
下面的代码需要box2d!!!!!!!!!!!
import org.jbox2d.common.vec2;
public class polyonutil {<br> private static polyonutil instance;<br> private float a;<br> private float b;<br> private boolean x_axis;<br> private boolean y_axis;<br> private float axis;<br> float i;
public static polyonutil getinstance() {<br> if (instance == null) {<br> instance = new polyonutil();<br> }<br> return instance;<br> }
public void setab(vec2 point1, vec2 point2) {<br> if (point1.x == point2.x) {<br> if (point1.y != point2.y) {<br> y_axis = true;<br> axis = point1.x;<br> }<br> } else if (point1.y == point2.y) {<br> if (point1.x != point2.x) {<br> x_axis = true;<br> axis = point1.y;<br> }<br> } else {<br> y_axis = false;<br> x_axis = false;<br> a = (point1.y - point2.y) / (point1.x - point2.x);<br> b = point1.y - point1.x * a;<br> }<br> }
public byte getflag(vec2 point) {<br> if (y_axis) {<br> if (point.x > axis) {<br> return 1;<br> } else if (point.x < axis) {<br> return -1;<br> } else {<br> return 0;<br> }
} else if (x_axis) {<br> if (point.y > axis) {<br> return 1;<br> } else if (point.y < axis) {<br> return -1;<br> } else {<br> return 0;<br> }<br> } else {<br> i = point.y - a * point.x - b;<br> if ((point.y - a * point.x - b) < 0) {<br> return -1;<br> } else if ((point.y - a * point.x - b) > 0) {<br> return 1;<br> } else {<br> return 0;<br> }<br> }<br> }<br>}
//下面的函数判断一群点是否构成凸多边形
public boolean ishull(vec2[] mousestroke){
int mousestrokelength=mousestroke.length;
for (int i = 0; i < mousestrokelength; i+=2) {<br> polyonutil.getinstance().setab(mousestroke[(i) % mousestrokelength],<br> mousestroke[(i + 1) % mousestrokelength]);<br> for (int j = 0, k = 0; j < mousestrokelength; j++) {<br> if (j == i || j == (i + 1)) {<br> continue;<br> } else {<br> flag[k] = polyonutil.getinstance().getflag(mousestroke[j<br> % mousestrokelength]);<br> k++;<br> }<br> }<br> for (int l = 0; l < flag.length - 1; l++) {<br> if (flag[l] * flag[l + 1] < 0) {<br> // ao 当是凹多边形时候<br> return false;<br> }<br> }<br> }
return true;
}
下面的代码需要box2d!!!!!!!!!!!
import org.jbox2d.common.vec2;
public class polyonutil {<br> private static polyonutil instance;<br> private float a;<br> private float b;<br> private boolean x_axis;<br> private boolean y_axis;<br> private float axis;<br> float i;
public static polyonutil getinstance() {<br> if (instance == null) {<br> instance = new polyonutil();<br> }<br> return instance;<br> }
public void setab(vec2 point1, vec2 point2) {<br> if (point1.x == point2.x) {<br> if (point1.y != point2.y) {<br> y_axis = true;<br> axis = point1.x;<br> }<br> } else if (point1.y == point2.y) {<br> if (point1.x != point2.x) {<br> x_axis = true;<br> axis = point1.y;<br> }<br> } else {<br> y_axis = false;<br> x_axis = false;<br> a = (point1.y - point2.y) / (point1.x - point2.x);<br> b = point1.y - point1.x * a;<br> }<br> }
public byte getflag(vec2 point) {<br> if (y_axis) {<br> if (point.x > axis) {<br> return 1;<br> } else if (point.x < axis) {<br> return -1;<br> } else {<br> return 0;<br> }
} else if (x_axis) {<br> if (point.y > axis) {<br> return 1;<br> } else if (point.y < axis) {<br> return -1;<br> } else {<br> return 0;<br> }<br> } else {<br> i = point.y - a * point.x - b;<br> if ((point.y - a * point.x - b) < 0) {<br> return -1;<br> } else if ((point.y - a * point.x - b) > 0) {<br> return 1;<br> } else {<br> return 0;<br> }<br> }<br> }<br>}
//下面的函数判断一群点是否构成凸多边形
public boolean ishull(vec2[] mousestroke){
int mousestrokelength=mousestroke.length;
for (int i = 0; i < mousestrokelength; i+=2) {<br> polyonutil.getinstance().setab(mousestroke[(i) % mousestrokelength],<br> mousestroke[(i + 1) % mousestrokelength]);<br> for (int j = 0, k = 0; j < mousestrokelength; j++) {<br> if (j == i || j == (i + 1)) {<br> continue;<br> } else {<br> flag[k] = polyonutil.getinstance().getflag(mousestroke[j<br> % mousestrokelength]);<br> k++;<br> }<br> }<br> for (int l = 0; l < flag.length - 1; l++) {<br> if (flag[l] * flag[l + 1] < 0) {<br> // ao 当是凹多边形时候<br> return false;<br> }<br> }<br> }
return true;
}
[2] Java 推算日期(计算距今多少年,几多月,多少天的日期)
来源: 互联网 发布时间: 2014-02-18
Java 推算日期(计算距今多少年,多少月,多少天的日期)
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public final class Utils { /** * get any time which you want before or after today. * @param year by number years(before or after this year) default:0 * @param month by number months(before or after this month)default:0,no change * @param day by number day(before or after this day)default:0,no change * @return */ public static Date getCountDate(int year, int month, int day){ Calendar cal = Calendar.getInstance (); cal.add (Calendar.YEAR, year); cal.add (Calendar.MONDAY, month); cal.add (Calendar.DATE, day); return cal.getTime (); } public static String dateToString (Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String temp = format.format (date); return temp; } public static void main(String[] args){ Date today = Utils.getCountDate (0, 0, 0); Date oneYearBefore = Utils.getCountDate (-1, 0, 0); Date oneMonthBefore = Utils.getCountDate (0, -1, 0); Date oneDayBefore = Utils.getCountDate (0, 0, -1); Date oneYearAfter = Utils.getCountDate (1, 0, 0); Date oneMonthAfter = Utils.getCountDate (0, 1, 0); Date oneDayAfter = Utils.getCountDate (0, 0, 1); System.out.println (today + "\t" + Utils.dateToString (today)); System.out.println (oneYearBefore + "\t" + Utils.dateToString (oneYearBefore)); System.out.println (oneMonthBefore + "\t" + Utils.dateToString (oneMonthBefore)); System.out.println (oneDayBefore + "\t" + Utils.dateToString (oneDayBefore)); System.out.println (oneYearAfter + "\t" + Utils.dateToString (oneYearAfter)); System.out.println (oneMonthAfter + "\t" + Utils.dateToString (oneMonthAfter)); System.out.println (oneDayAfter + "\t" + Utils.dateToString (oneDayAfter)); } }
Result:
Mon Oct 24 16:09:45 CST 2011 2011-10-24
Sun Oct 24 16:09:45 CST 2010 2010-10-24
Sat Sep 24 16:09:45 CST 2011 2011-09-24
Sun Oct 23 16:09:45 CST 2011 2011-10-23
Wed Oct 24 16:09:45 CST 2012 2012-10-24
Thu Nov 24 16:09:45 CST 2011 2011-11-24
Tue Oct 25 16:09:45 CST 2011 2011-10-25
[3] 取ARGB色彩的分度值
来源: 互联网 发布时间: 2014-02-18
取ARGB颜色的分度值
private static int getR32(int c) { return (c >> 0) & 0xFF; } // access the red component from a premultiplied color private static int getG32(int c) { return (c >> 8) & 0xFF; } // access the red component from a premultiplied color private static int getB32(int c) { return (c >> 16) & 0xFF; } // access the red component from a premultiplied color private static int getA32(int c) { return (c >> 24) & 0xFF; }
最新技术文章: