Compass Reader Basic by 祥瑞
利用compass sensor, 在螢幕顯示亙指北方的箭頭
import lejos.nxt.*;
import lejos.nxt.addon.CompassSensor;
import lejos.util.Delay;
import javax.microedition.lcdui.Graphics;
class Compass {
public static CompassSensor compass = new CompassSensor(SensorPort.S1);
public static int lineLength = 50; //線長度
public static int arrowLength = 15; //箭頭線長度
public static int arrowAng = 30; //箭頭線與線的角度
public static int dots = 12; //圓周點的數目
public static int delay = 150; //更新螢幕間隔
public static int offsetX;
public static int offsetY;
public static Graphics g = new Graphics();
public static double direction;
public static void main(String args[]) {
Button.ESCAPE.addButtonListener(new ButtonListener()
{
public void buttonPressed(Button b){System.exit(0);}
public void buttonReleased(Button b){}
});
while(true) {
direction = compass.getDegreesCartesian();
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), 100-getLocationX(direction, lineLength), 64-getLocationY(direction, lineLength));
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), getLocationX(direction, lineLength) + (int)(Math.cos(Math.toRadians(direction+90-arrowAng))*arrowLength), getLocationY(direction, lineLength) + (int)(Math.sin(Math.toRadians(direction+90-arrowAng))*arrowLength));
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), getLocationX(direction, lineLength) - (int)(Math.cos(Math.toRadians(90-direction-arrowAng))*arrowLength), getLocationY(direction, lineLength) + (int)(Math.sin(Math.toRadians(90-direction-arrowAng))*arrowLength));
g.drawString("N", getLocationX(direction, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("W", getLocationX(direction+270, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+270, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("E", getLocationX(direction+90, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+90, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("S", getLocationX(direction+180, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+180, lineLength+10)-LCD.FONT_HEIGHT/2);
for(int i=0; i<360; i+=360/dots) g.drawLine(getLocationX(direction+i, lineLength), getLocationY(direction+i, lineLength), getLocationX(direction+i, lineLength-5), getLocationY(direction+i, lineLength-5));
Delay.msDelay(delay);
LCD.clear();
}
}
public static int getLocationX(double ang, int length) {
return 50 + (int)(Math.sin(Math.toRadians(ang))*length/2);
}
public static int getLocationY(double ang, int length) {
return 32 - (int)(Math.cos(Math.toRadians(ang))*length/2);
}
}
import lejos.nxt.*;
import lejos.nxt.addon.CompassSensor;
import lejos.util.Delay;
import javax.microedition.lcdui.Graphics;
class Compass {
public static CompassSensor compass = new CompassSensor(SensorPort.S1);
public static int lineLength = 50; //線長度
public static int arrowLength = 15; //箭頭線長度
public static int arrowAng = 30; //箭頭線與線的角度
public static int dots = 12; //圓周點的數目
public static int delay = 150; //更新螢幕間隔
public static int offsetX;
public static int offsetY;
public static Graphics g = new Graphics();
public static double direction;
public static void main(String args[]) {
Button.ESCAPE.addButtonListener(new ButtonListener()
{
public void buttonPressed(Button b){System.exit(0);}
public void buttonReleased(Button b){}
});
while(true) {
direction = compass.getDegreesCartesian();
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), 100-getLocationX(direction, lineLength), 64-getLocationY(direction, lineLength));
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), getLocationX(direction, lineLength) + (int)(Math.cos(Math.toRadians(direction+90-arrowAng))*arrowLength), getLocationY(direction, lineLength) + (int)(Math.sin(Math.toRadians(direction+90-arrowAng))*arrowLength));
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), getLocationX(direction, lineLength) - (int)(Math.cos(Math.toRadians(90-direction-arrowAng))*arrowLength), getLocationY(direction, lineLength) + (int)(Math.sin(Math.toRadians(90-direction-arrowAng))*arrowLength));
g.drawString("N", getLocationX(direction, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("W", getLocationX(direction+270, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+270, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("E", getLocationX(direction+90, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+90, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("S", getLocationX(direction+180, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+180, lineLength+10)-LCD.FONT_HEIGHT/2);
for(int i=0; i<360; i+=360/dots) g.drawLine(getLocationX(direction+i, lineLength), getLocationY(direction+i, lineLength), getLocationX(direction+i, lineLength-5), getLocationY(direction+i, lineLength-5));
Delay.msDelay(delay);
LCD.clear();
}
}
public static int getLocationX(double ang, int length) {
return 50 + (int)(Math.sin(Math.toRadians(ang))*length/2);
}
public static int getLocationY(double ang, int length) {
return 32 - (int)(Math.cos(Math.toRadians(ang))*length/2);
}
}
留言 💬 (0)
還沒有留言,來當第一個。