As we are in a compulson to use float point and trigonometric functions. As we need to use CLDC 1.1 based classes for just this calculation which slides down the number of devices supported, I have framed a simple function which gives you the coordinates of a circle when radius and angle are given...
This class can be used when an object has to move in a circular path based on angle... say we are using pendulam like motion in our game then we would badly need such physics. And this class saves much CPU but its little bad in memory usage....
- Code: Select all
/*
* calc.java
*
* Created on August 30, 2007, 4:49 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author nharishankar
*/
public class calc {
private static int[] val = new int[] {
2,100, 3,100, 5,100, 7,100,
9,100, 10,99, 12,99, 14,99,
16,99, 17,98, 19,98, 21,98,
22,97, 24,97, 26,97, 28,96,
29,96, 31,95, 33,95, 34,94,
36,93, 37,93, 39,92, 41,91,
42,91, 44,90, 45,89, 47,88,
48,87, 50,87, 51,86, 53,85,
54,84, 56,83, 57,82, 59,81,
60,80, 62,79, 63,78, 64,77,
66,75, 67,74, 68,73, 69,72,
71,71, 72,69, 73,68, 74,67,
75,66, 77,64, 78,63, 79,62,
80,60, 81,59, 82,57, 83,56,
84,55, 85,53, 86,52, 87,50,
87,49, 88,47, 89,45, 90,44,
91,42, 91,41, 92,39, 93,38,
93,36, 94,34, 95,33, 95,31,
96,29, 96,28, 97,26, 97,24,
97,23, 98,21, 98,19, 98,17,
99,16, 99,14, 99,12, 99,11,
100,9, 100,7, 100,5, 100,4,
100,2, 100,0, 100,-2, 100,-3,
100,-5, 100,-7, 100,-9, 99,-10,
99,-12, 99,-14, 99,-16, 98,-17,
98,-19, 98,-21, 97,-22, 97,-24,
97,-26, 96,-27, 96,-29, 95,-31,
95,-32, 94,-34, 93,-36, 93,-37,
92,-39, 91,-41, 91,-42, 90,-44,
89,-45, 88,-47, 88,-48, 87,-50,
86,-51, 85,-53, 84,-54, 83,-56,
82,-57, 81,-59, 80,-60, 79,-61,
78,-63, 77,-64, 76,-66, 74,-67,
73,-68, 72,-69, 71,-71, 70,-72,
68,-73, 67,-74, 66,-75, 64,-77,
63,-78, 62,-79, 60,-80, 59,-81,
57,-82, 56,-83, 55,-84, 53,-85,
52,-86, 50,-87, 49,-87, 47,-88,
46,-89, 44,-90, 42,-91, 41,-91,
39,-92, 38,-93, 36,-93, 34,-94,
33,-95, 31,-95, 29,-96, 28,-96,
26,-97, 24,-97, 23,-97, 21,-98,
19,-98, 18,-98, 16,-99, 14,-99,
12,-99, 11,-99, 9,-100, 7,-100,
5,-100, 4,-100, 2,-100, 0,-100,
-2,-100, -3,-100, -5,-100, -7,-100,
-9,-100, -10,-99, -12,-99, -14,-99,
-15,-99, -17,-99, -19,-98, -21,-98,
-22,-97, -24,-97, -26,-97, -27,-96,
-29,-96, -31,-95, -32,-95, -34,-94,
-36,-93, -37,-93, -39,-92, -41,-91,
-42,-91, -44,-90, -45,-89, -47,-88,
-48,-88, -50,-87, -51,-86, -53,-85,
-54,-84, -56,-83, -57,-82, -59,-81,
-60,-80, -61,-79, -63,-78, -64,-77,
-65,-76, -67,-74, -68,-73, -69,-72,
-71,-71, -72,-70, -73,-68, -74,-67,
-75,-66, -76,-64, -78,-63, -79,-62,
-80,-60, -81,-59, -82,-58, -83,-56,
-84,-55, -85,-53, -86,-52, -86,-50,
-87,-49, -88,-47, -89,-46, -90,-44,
-91,-42, -91,-41, -92,-39, -93,-38,
-93,-36, -94,-34, -94,-33, -95,-31,
-96,-29, -96,-28, -97,-26, -97,-24,
-97,-23, -98,-21, -98,-19, -98,-18,
-99,-16, -99,-14, -99,-12, -99,-11,
-100,-9, -100,-7, -100,-5, -100,-4,
-100,-2, -100,0, -100,2, -100,3,
-100,5, -100,7, -100,8, -99,10,
-99,12, -99,14, -99,15, -99,17,
-98,19, -98,21, -97,22, -97,24,
-97,26, -96,27, -96,29, -95,31,
-95,32, -94,34, -93,36, -93,37,
-92,39, -91,40, -91,42, -90,44,
-89,45, -88,47, -88,48, -87,50,
-86,51, -85,53, -84,54, -83,56,
-82,57, -81,59, -80,60, -79,61,
-78,63, -77,64, -76,65, -74,67,
-73,68, -72,69, -71,71, -70,72,
-68,73, -67,74, -66,75, -64,76,
-63,78, -62,79, -60,80, -59,81,
-58,82, -56,83, -55,84, -53,85,
-52,86, -50,86, -49,87, -47,88,
-46,89, -44,90, -43,91, -41,91,
-39,92, -38,93, -36,93, -34,94,
-33,94, -31,95, -30,96, -28,96,
-26,97, -24,97, -23,97, -21,98,
-19,98, -18,98, -16,99, -14,99,
-12,99, -11,99, -9,100, -7,100,
-6,100, -4,100, -2,100, 0,100
};
private static int sin(int angle) {
if(angle >= 0 && angle < 360)
return val[angle * 2];
else
return 0;
}
private static int cos(int angle) {
if(angle >= 0 && angle < 360)
return val[(angle * 2) + 1];
else
return 0;
}
public static int circlex(int radius, int angle) {
return (cos(angle) * radius)/100;
}
public static int circley(int radius, int angle) {
return (sin(angle) * radius)/100;
}
}
Sample code to show you how to move a pixel in a circle path using my class
- Code: Select all
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/*
* Demo.java
*
* Created on August 30, 2007, 4:42 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author nharishankar
*/
public class Demo extends Canvas{
private int angle = 0;
/** Creates a new instance of Demo */
public Demo() {
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
angle++;
if(angle > 360)
angle = 0;
putPixel(calc.circlex(100, angle), calc.circley(100, angle), g);
repaint();
try {
Thread.currentThread().sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
private void putPixel(int x, int y, Graphics g) {
g.setColor(0);
g.drawLine(getWidth()/2 + x, getHeight()/2 + y, getWidth()/2 + x, getHeight()/2 + y);
}
}
