利用Math.cos和Math.sin計算當前角度所對應方位
根據Math.cos和Math.sin來實現不同的方位判斷,想著藉助該Demo可以做成時鐘計時的小事例、類似於彈彈堂中的調整炮彈發射方向的小Demo。主要根據下面函式來計算:
var angleRadians:Number = angle * Math.PI / 180; //換算成弧度
var p:Point = new Point(Math.cos(angleRadians) * RADIUS, Math.sin(angleRadians) * RADIUS); //計算偏移量
AS3中的示例程式碼:
package mytest
{
import flash.display.Sprite;
import flash.geom.Point;
public class MyTest extends Sprite
{
private const CENTRE:Point = new Point(100, 150); //圓心
private const RADIUS:Number = 100; //半徑
private const START_ANGLE:Number = 15; //起初角度
private const Tile_TOTAL:Number = 9; //元件個數
private const
public function MyTest()
{
for(var i:uint=0; i<Tile_TOTAL; i++){
var pb:Tile = new Tile(); //新建元件
var angle:uint = i * SINGLE_ANGLE + START_ANGLE; //角度
var p:Point = getPoint(angle);
pb.x = p.x; pb.y = p.y; //移動元件
pb.rotation = angle - 90; //旋轉元件
this.addChild(pb);//加到舞臺
}
}
private function getPoint(angle:uint):Point{
var angleRadians:Number = angle * Math.PI / 180; //換算成弧度
var p:Point = new Point(Math.cos(angleRadians) * RADIUS, Math.sin(angleRadians) * RADIUS); //計算偏移量
p.x += CENTRE.x; //跟據圓心對偏移量進行修正
p.y += CENTRE.y; //跟據圓心對偏移量進行修正
return p;
}
}
}
package mytest
{
import flash.display.Sprite;
public class Tile extends Sprite
{
public function Tile()
{
super();
graphics.lineStyle(1, 0x0000ff);
graphics.beginFill(0xcccccc,0.5);
graphics.endFill();
graphics.drawRect(0, 0, 50, 20);
}
}
}
AS3效果圖:
JAVA中的示例程式碼:
/**一個時鐘Demo的雛形*/
package mychTest;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
public class EclipsePaint extends Applet{
private Graphics drawOffScreen;
private Image offScreen;
private int appWidth, appHeight;
private Clock clock; //鐘錶實體類
public void init(){
appWidth = getSize().width;
appHeight = getSize().height;
offScreen = createImage(appWidth, appHeight);
drawOffScreen = offScreen.getGraphics();
clock = new Clock(appWidth, appHeight);
new Thread(new PaintThread()).start();
}
public void paint(Graphics g){
clock.draw(drawOffScreen);
g.drawImage(offScreen, 0, 0, this);
}
public void update(Graphics g){
paint(g);
}
class PaintThread implements Runnable{
@Override
public void run() {
while(true){
try {
Thread.sleep(41);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}
}
class Clock{
private int x, y, width, height, appWidth, appHeight; //外圓框的基本資訊
private int heartX, heartY, heartWidth, heartHeight; //圓心座標和寬度、高度
private int minuteAimX, minuteAimY; //秒針的X,Y座標
private double minuteAngle; //秒針的當前角度
public Clock(int appWidth, int appHeight){
this.appWidth = appWidth;
this.appHeight = appHeight;
width = 80; //外圓直徑,記住是直徑
height = 80;
x = appWidth / 2 - width / 2; //繪製外圓的X座標
y = appHeight / 2 - height / 2; //繪製外圓的Y座標
heartWidth = 5; //圓心直徑
heartHeight = 5;
heartX = x + width / 2 - heartWidth/2; //繪製圓心的X座標
heartY = y + height / 2 - heartHeight/2; //繪製圓心的Y座標
minuteAngle = 60; //當前角度
}
public void draw(Graphics g){
//繪製外圓框
g.setColor(Color.RED);
g.drawOval(x, y, width, height);
//繪製中心點
g.setColor(Color.BLACK);
g.fillOval(heartX, heartY, heartWidth, heartHeight);
//繪製秒線
g.setColor(Color.BLUE);
minuteAimX = (int)(Math.cos(minuteAngle * Math.PI / 180) * width/2) + (x + width / 2);
minuteAimY = (int)(Math.sin(minuteAngle * Math.PI / 180) * height/2) + (y + height / 2);
g.drawLine((x + width / 2), (y + height / 2), minuteAimX, minuteAimY);
//這裡的(x + width / 2), (y + height / 2)相當於圓心的X座標,Y座標; 因為heartX, heartY減去了圓心的半徑,所以只是適用於繪製的時候
}
}
JAVA效果圖:
相關推薦
利用Math.cos和Math.sin計算當前角度所對應方位
根據Math.cos和Math.sin來實現不同的方位判斷,想著藉助該Demo可以做成時鐘計時的小事例、類似於彈彈堂中的調整炮彈發射方向的小Demo。主要根據下面函式來計算: var angleRadians:Number = angle * Math.PI / 180
Math.cos()和Math.sin()和Math.atan()
數學上: cos(60°) = 0.5 js: Math.cos(Math.PI/180*60) = 0.5000000000000001 原因: 近似值,為什麼不直接為0.5 呢?因為Math.PI其實也只是PI的近似值,如果是PI的精確值的話,結果肯定是0.5了。 數
C#取整函數Math.Round、Math.Ceiling和Math.Floor
取整 c# mat logs color pre log clas 偶數 1.Math.Round:四舍六入五取偶 引用內容 Math.Round(0.0) //0 Math.Round(0.1) //0 Math.Round(0.2) //0 Math.Round(0
復習C#的方法Math.Max和Math.Min
log ins get 接下來 .html 但是 n) 最小 2個 溫故而知新,今天學習Math.Max和Min的方法。這2個方法,均需要傳入2個參數,返回參數中最大值和最小值。 class Ac { public void L
Math.ceil()、Math.floor()和Math.round()
分享 .com 方法 com ID inf 整數 分享圖片 img 下面來介紹將小數值舍入為整數的幾個方法:Math.ceil()、Math.floor()和Math.round()。 這三個方法分別遵循下列舍入規則: Math.ceil()執行向上舍入,即它總是將數值向
java中默認lang包下的Math.round和Math.rint的區別
public 的區別 pub round tro sta long ava 最小 public static double rint ( double ): 取最接近的整數(若相同則取偶數),然後轉為 double 類型 public static int round (
數學API Math.atan() 和Math.atan2() 三角函式複習
今天在學習貝塞爾曲線看到需要結合三角函式 以及兩個不認識的Api :API Math.atan() 和Math.atan2() 先看下三角函式 正切函式圖:(180為一個週期 即45=45+180) 正弦 正餘弦函式方程為: y = As
Java中Math.floor()和Math.ceil()的區別
Math.ceil():將小數部分一律向整數部分進位 簡單來說,當小數部分為非0值時,進為1.0 Math.ceil(12.1)=13 Math.ceil(12.8)=13 當小數部分為0時,算作0.0 Math.ceil(12.0)=12 Math.ceil(10.0)=
JS Math.sin() 與 Math.cos() 用法
1.關於Math.sin(x)以及math.cos(X) Math.sin(x) x 的正玄值。返回值在 -1.0 到 1.0 之間; Math.cos(x)  
JS 中 Math 物件的【三角函式】的用法與實戰例子【打靶遊戲】 | Math.tan(), Math.cos(), Math.sin()
不少人在學習JavaScript的時候,都知道有那麼一個用於數學計算的類 Math,現在來簡單回顧以下,強大的 Math 類中,有哪些屬性和哪些方法: 如果用JS做前端的時候,就會發現跟 Mat
js計算當前日期上一個月和下一個月
pre 日期 下一個 測試 -s digg rem bind popu /** * 獲取上一個月 * * @date 格式為yyyy-mm-dd的日期,如:2014-01-25 */ functi
JavaScript中Math.max.apply()和Math.max()的區別
作用 div 調用 class color his arr array this JavaScript中Math.max()方法可以求出給定參數中的最大值,給定參數≥2個,可以使多個,但是必須是數字。 > Math.max(1,2,3,5,9,4); < 9
JS基礎8-常用內置對象(Date和Math)
minutes 內置 sqrt eight time OS second con tab 一、Date對象 1.Date對象的創建 new Date() new Date(month dd,yyyy hh:mm:ss) new Date(yyyy,mth,dd,hh,mm,
math類和biginteger類
array max 1.0 new 調用 向上取整 最小 pri ceil Math類:這種工具類,一般不會創建對象,方法為靜態方法,直接調用 package com.oracle.demo02; public class MathDemo { public
js math 對數和指數處理 expm1 log1p
pan style 應用 clas bsp mic ini exp spa 1、Math.expm1() Math.expm1(x)返回 ex - 1,即Math.exp(x) - 1。 Math.expm1(-1) // -0.6321205588285577
js--日期和Math、陣列和物件API
一、日期 Date.now() //獲取當前時間毫秒數,從1970年到現在的毫秒數 var dt =new Date() dt.getTime() //獲取毫秒數 dt.getFullYear() //年 dt.getMonth() //月(0-11) dt.ge
Js內建物件(global和Math)
Global物件(全域性物件) 1、encodeURI(URIString) 功能:將字串作為URI進行編碼,返回值為URIstring 的副本。 2.encodeURIComponent(URIString) 功能:將字串作為URI元件進行編碼,返回值為URIstring的副本。
Js中apply和Math.max()函式的問題及區別
這篇文章主要介紹了js中apply和Math.max()函式的問題,本文給大家帶來兩種答案,每一種答案給大家介紹的非常詳細,在文章底部給大家提到了js中Math.max.apply和Math.max的區別,寫的十分的全面細緻,具有一定的參考價值,對此有需要的朋友可以參考學習下。如有不足之處,歡迎批評指正。
Java-static和math類。
一.static A:static關鍵字的特點 a:隨著類的載入而載入 b:優先於物件存在 c:被類的所有物件共享 舉例:咱們班級的學生應該共用同一個班級編號。 其實這個特點也是在告訴我們什麼時候使用靜態? 如果某個成員變數是被所有物件共享的,那麼它就應該定義為
常用的Date對象和Math對象方法
現在時間 date對象 value 本地 平方根 minutes 最小值 minute 取整 Date對象方法: 當前用戶本地時間 let time = new Date(); 獲取整數年 console.log(time.getFullYear()); 獲取當前月