实例编程解答打印正弦函数

lucima 2007-12-21 12:56:56
用*打印正弦函数图像。



我现在能实现一半



package src;
//打印正弦函数
import java.lang.StrictMath;
public class Sin {
public static void main(String[] args){
double x,y,m;
for(y=1;y>=-0;y-=0.1){
m=StrictMath.asin(y)*10;
for(x=1;x<m;x++)
System.out.print(" ");
System.out.print("*");
for(;x<31-m;x++)
System.out.print(" ");
System.out.println("*");

}
//下半部分没办法移动到正确的位置。

for(y=0;y>=-1;y-=0.1){

m=-(StrictMath.asin(y)*10);
for(x=0;x<m;x++)
System.out.print(" ");
System.out.print("*");
for(;x<31-m;x++)
System.out.print(" ");
System.out.println("*");

}

}


}




另外我想问问,在awt或者swing中有没有这样一种画曲线的函数。
...全文
356 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZengGuiDong 2010-07-29
  • 打赏
  • 举报
回复
谢谢你们,今天是我第一次进入这个网站,也是第一次发帖。没想到得到的回复速度这么快,,真的很谢谢。
  • 打赏
  • 举报
回复
贴了个小一点的。


#### | ####
## ## | ## ##
# # | # #
# # | # #
# # | # #
# # | # #
# # | # #
|
# # | # #
# # | # #
# # |# #
|
#------------------------#------------------------#------------------------#------------------------#
|
# #| # #
# # | # #
# # | # #
|
# # | # #
# # | # #
# # | # #
# # | # #
# # | # #
## ## | ## ##
#### | ####


### ##### ###
## ## | ## ##
# # | # #
# # | # #
# # | # #
|
# # | # #
# # | # #
# # | # #
|
# # | # #
# # | # #
--------------------------------------------------+--------------------------------------------------
# # | # #
# # | # #
|
# # | # #
# # | # #
# # | # #
|
# # | # #
# # | # #
# # | # #
## ## | ## ##
##### | #####
  • 打赏
  • 举报
回复
填充字符采用“#”的话,更为漂亮一些,呵呵~~
  • 打赏
  • 举报
回复
终于完成了,呵呵,头好晕~~

public class Test {
public static void main(String[] args) {
TriFunc tri = new TriFunc();

// 生成一块25×100的画布
Canvas canvas = new Canvas(25, 120);

// 画sin曲线,周期为2
tri.drawSin(canvas, 2.0);
canvas.printCanvas();

System.out.println();
canvas.reset();
// 画cos曲线,周期为2
tri.drawCos(canvas, 2.0);
canvas.printCanvas();
}
}

class TriFunc {

/**
* 画sin曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawSin(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
// x 轴的比率
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
// y 轴的放大倍率
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i < canvas.getWidth(); i++) {
// 将数组索引映射为横坐标值
double k = (i - canvas.getWidth() / 2) * xRatio;
// 将sin值映射为数组索引
int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}

/**
* 画cos曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawCos(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i < canvas.getWidth(); i++) {
double k = (i - canvas.getWidth() / 2) * xRatio;
int h = yMulti - (int)Math.round(Math.cos(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
}


class Canvas {

private int height;
private int width;
private char[][] canvas;

// 填充字符
public static char FILL_CHAR = '+';
// 空白字符
public static char BLANK_CHAR = ' ';

/**
* 构建一块画布
* @param height
* @param width
*/
public Canvas(int height, int width) {
// 由于需要画坐标轴,所以得采用奇数
this.height = height % 2 == 0 ? height + 1 : height;
this.width = width % 2 == 0 ? width + 1 : width;
init();
}

/**
* 初始化画布
*/
private void init() {
this.canvas = new char[height][width];
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
canvas[i][j] = BLANK_CHAR;
}
}
addAxis();
}

/**
* 添加坐标轴
*/
private void addAxis() {
// 添加横坐标
int y = height / 2;
for(int x = 0; x < width; x++) {
canvas[y][x] = '-';
}
// 添加纵坐标
int xx = width / 2;
for(int yy = 0; yy < height; yy++) {
canvas[yy][xx] = '|';
}
// 添加原点
canvas[y][xx] = '+';
}

/**
* 输出画布
*/
public void printCanvas() {
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
System.out.print(canvas[i][j]);
}
System.out.println();
}
}

/**
* 清空画布
*/
public void reset() {
init();
}

public int getHeight() {
return height;
}
public int getWidth() {
return width;
}

public char[][] getCanvas() {
return canvas;
}
}


本想贴个输出示例,但是这里贴不下了,可以自己试试看的。
  • 打赏
  • 举报
回复
我在我的博客上有个五角星的示例,有兴趣的话可以看看。

http://blog.csdn.net/bao110908/archive/2007/11/27/1904452.aspx

正、余弦函数的,我再来回想一下。
  • 打赏
  • 举报
回复
我原来做过,还带有坐标轴的,我记得是用字符数组做的。
lucima 2007-12-21
  • 打赏
  • 举报
回复
package src;
//打印正弦函数
import java.lang.StrictMath;
public class Sin {
public static void main(String[] args){
double x,y,m;
for(y=1;y>=-0;y-=0.1){
m=StrictMath.asin(y)*10;
for(x=1;x<m;x++)
System.out.print(" ");
System.out.print("*");
for(;x<31-m;x++)
System.out.print(" ");
System.out.println("*");

}

//后半周期函数图形
for(y=0;y>=-1;y-=0.1){

m=31-(StrictMath.asin(y)*10);
for(x=0;x<=m;x++)
System.out.print(" ");
System.out.print("*");
for(x=StrictMath.abs(StrictMath.asin(y)*10);x<31-StrictMath.abs(StrictMath.asin(y)*10);x++)
System.out.print(" ");
System.out.println("*");

}

}


}
试验了一整天的代码。好麻烦,不知道大家有没有简单易行的办法

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧