?
問題:
由上一篇? 趣味編程 - 繪制余弦曲線 ?思考正弦曲線的繪制.
分析: 正弦曲線剛好是余弦曲線關于x軸翻轉后的圖形. 圖形的橫坐標取值范圍x∈( -180°,180°]
不過此時的圖形不是常規的正弦形狀.
算法: 將上一篇? 趣味編程 - 繪制余弦曲線 ?中實現程序 for (Y= 10 ;?Y>=- 10 ;?Y--) 換成 for (Y=- 10 ;?Y< 10 ;?Y++)
實現程序 ①
?
/* @TODO 繪制正弦曲線 @author jarg http://jarg.iteye.com/ */ import static java.lang.System.*; import static java.lang.Math.*; public class SIN { private static final int MULTI = 12; // 圖形放大倍數 private static final int LEN = (int)(acos(-1)*MULTI)*2; // 圖形橫坐標長度 private static int X; // 橫坐標 private static int Y; // 縱坐標 public static void main(String[] args) { display(); } public static void display() { for(Y=-10; Y<10; Y++) { X = (int)(acos((double)Y/10)*MULTI); for(int m=0; m<X; m++) { out.print(" "); } out.print("*"); for(int n=X; n<LEN-X; n++) { out.print(" "); } out.println("*"); } } }?
? ?
??
實現程序②
?
分析: 分二種情況,將Y值大于等于0與小于0的圖形分開輸出.上一 圖形的橫坐標取值范圍x∈(-18 0°,180°],而現在輸出 圖形的橫坐標取值范圍x∈( 0°,360°]
?
算法: 定義一個flag標記,當Y<0時,將X坐標向右移動半個長度的圖形橫坐標總長度.
?
/* @TODO 繪制正弦曲線2 @author jarg http://jarg.iteye.com/ */ import static java.lang.System.*; import static java.lang.Math.*; public class SIN2 { private static final int MULTI = 12; // 圖形放大倍數 private static final int LEN = (int)(acos(-1)*MULTI)*2; // 圖形橫坐標長度 private static int X; // 橫坐標 private static int Y; // 縱坐標 public static void main(String[] args) { display(); } public static void display() { int flag = 0; // 為了在Y負坐標情況,輸出位置右移LEN/2 for(Y=10; Y>=-10; Y--) { flag = (Y<0)?1:0; /* 由于Y<0時asin值為負,所以應當取其絕對值 */ X = abs((int)(asin((double)Y/10)*MULTI)) + flag*LEN/2; for(int m=0; m<X; m++) { out.print(" "); } out.print("*"); for(int n=X+1; n<LEN/2 - X + LEN*flag; n++) { out.print(" "); } out.println("*"); } } }
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
