「正弦函数java」求正弦型函数

博主:adminadmin 2022-12-11 12:42:05 58

今天给各位分享正弦函数java的知识,其中也会对求正弦型函数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么取正弦和余弦函数

java.lang.Math类

Math中的方法

double b;

b=sin(double a) 返回a角的三角正弦。

b=cos(double a) 返回a角的三角余弦。

用java语言绘制正弦曲线

很简单,程序写给你,给分:

---------------------------------------------

import java.awt.*;

import javax.swing.JFrame;

import java.util.Random;

import java.text.DecimalFormat;

public class SinDemo extends JFrame {

private double cx = 1, cy = 1;

private double toCx = 1, toCy = 1;

private Random rnd = new Random();

private DecimalFormat df = new DecimalFormat("0.00");

private SinDemo () {

super("Sin-Demo");

setSize(600, 600);

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

run();

}

private int translateX (double x) {

return (int)(x*getWidth()/Math.PI/4 + getWidth()/2);

}

private int translateY (double y) {

return (int)(getHeight()/2 - y*getWidth()/Math.PI/4);

}

private double sin (double x) {

return (cy * Math.sin(cx * x));

}

@Override

public void paint (Graphics g) {

super.paint(g);

g.setColor(Color.BLUE);

g.drawString("y = " + df.format(cx) + " * sin( " + df.format(cy) + " * x)", 50, 50);

g.setColor(Color.LIGHT_GRAY);

g.drawLine(0, getHeight()/2, getWidth(), getHeight()/2);

g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight());

g.setColor(Color.BLACK);

for (double i=-Math.PI/cx; iMath.PI/cx; i+=0.1)

g.drawLine(translateX(i), translateY(sin(i)),

translateX(i+0.1), translateY(sin(i+0.1)));

}

public void run () {

while (true) {

if (Math.abs(cx - toCx)  0.1) {

toCx = rnd.nextDouble()*2;

toCy = rnd.nextDouble()*2;

}

cx += (toCx - cx)/50;

cy += (toCy - cy)/50;

paint(getGraphics());

try {

Thread.sleep(80);

} catch (InterruptedException ie) {}

}

}

public static void main (String args[]) {

new SinDemo();

}

}

-------------------------------------------------

再给你一个截图:

java绘制动态正弦函数

package OnlineUserCount;

import java.awt.*;

import javax.swing.*;

public class Sin extends JPanel{

private double x;

private double y;

@Override

protected void paintComponent(Graphics g) {

// TODO Auto-generated method stub

super.paintComponent(g);

g.setColor(Color.WHITE);//设置面板背景色

g.fillRect(0, 0, 400, 300);//填充面板

g.setColor(Color.RED);//设置画线的颜色

for(x=0;x=360;x+=0.1)//一个周期

{

y=Math.sin(x*Math. PI/180);//转化为弧度,1度=π/180弧度

y=(100+80*y);//便于在屏幕上显示

//g.drawString(".",(int)x,(int)y);//用这种方式也可以

g.drawLine((int)x, (int)y, (int)x,(int) y);//画点

}

}

public static void main(String []args){

Sin s= new Sin();

JFrame j=new JFrame();

j.setTitle("一个周期的正弦曲线");

j.add(s);

j.setSize(400, 300);

j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

j.setVisible(true);

}

}

//效果截图

正弦函数java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于求正弦型函数、正弦函数java的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-11,除非注明,否则均为首码项目网原创文章,转载请注明出处。