java用画正弦函数_java – 绘制正弦和余弦函数

这篇博客介绍了一个Java程序,用于绘制红色的正弦函数和蓝色的余弦函数。作者通过DrawSine类实现了图形绘制,利用Math.sin()和Math.cos()函数计算点的坐标,并使用Graphics类的drawPolyline方法连接这些点。虽然正弦函数部分已经完成,但余弦函数的代码缺失,作者寻求帮助以完善程序并添加-Pi和Pi的标记到图表上。
摘要由CSDN通过智能技术生成

我目前在做作业方面遇到了一些问题.

这是练习:

(Plot the sine and cosine functions) Write a program that plots the sine function in red and the cosine function in blue.

hint: The Unicode for Pi is \u03c0. To display -2Pi, use g.drawString(“-2\u03c0”, x, y). For a trigonometric function like sin(x), x is in radians. Use the following loop to add the points to a polygon p

for (int x = -170; x <= 170; x++) {

p.addPoint(x + 200, 100 - (int)(50 * Math.sin((x / 100.0) * 2 * Math.PI)));

-2Pi is at (100, 100), the center of the axis is at (200, 100), and 2Pi is at (300, 100)

Use the drawPolyline method in the Graphics class to connect the points.

好吧,所以我所拥有的sin函数与练习中的函数略有不同,但是它起作用所以它应该不是问题.另一方面,余弦函数,我无法找到它的代码所以我没有在我的程序中.

我还需要做的是将-Pi和Pi放在他们可敬的地方的图表上.

所以,这是代码.

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.Polygon;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {

setLayout(new BorderLayout());

add(new DrawSine(), BorderLayout.CENTER);

}

public static void main(String[] args) {

Exercise13_12 frame = new Exercise13_12();

frame.setSize(400, 300);

frame.setTitle("Exercise13_12");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

class DrawSine extends JPanel {

double f(double x) {

return Math.sin(x);

}

double g(double y) {

return Math.cos(y);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawLine(10, 100, 380, 100);

g.drawLine(200, 30, 200, 190);

g.drawLine(380, 100, 370, 90);

g.drawLine(380, 100, 370, 110);

g.drawLine(200, 30, 190, 40);

g.drawLine(200, 30, 210, 40);

g.drawString("X", 360, 80);

g.drawString("Y", 220, 40);

Polygon p = new Polygon();

for (int x = -170; x <= 170; x++) {

p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2

* Math.PI)));

}

g.drawPolyline(p.xpoints, p.ypoints, p.npoints);

g.drawString("-2\u03c0", 95, 115);

g.drawString("2\u03c0", 305, 115);

g.drawString("0", 200, 115);

}

}

}

如果有人有时间帮助我,我将非常感激.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值