简体   繁体   English

在java中绘制sin(x)图

[英]Drawing sin(x) graph in java

I am currently trying to draw the graph of sin(x) in java. 我目前正在尝试在java中绘制sin(x)的图形。 I am required by the instructions of my assignment to only use drawLine() as the method of drawing the graph. 我的指令要求我只使用drawLine()作为绘制图形的方法。 I can't seem to figure out how to properly set my y value though. 我似乎无法弄清楚如何正确设置我的y值。 Right now what I have is a while loop used in order to draw the line pixel by pixel, but cannot manage to get the y value to be correct. 现在我所拥有的是一个while循环,用于逐个像素地绘制线条,但无法设法让y值正确。 Here is what I have so far. 这是我到目前为止所拥有的。

public class GraphJComponent extends JComponent {
public void paintComponent (Graphics g){
    Color axis = new Color(128, 128, 128);
    g.setColor(axis);

    int xShift = getWidth() / 50;
    int xShift2 = getWidth() / 100;
    int yShift = getHeight() / 10;
    int yShift2 = getHeight() / 17;

    g.drawLine(xShift,yShift,xShift,getHeight() - yShift);
    g.drawLine(xShift, getHeight() / 2, getWidth() - xShift, getHeight() / 2);

    g.drawString("0", xShift + xShift2, getHeight() / 2 + yShift2);
    g.drawString("1", xShift + xShift2, yShift - (yShift2 / 4));
    g.drawString("-1", xShift + xShift2, getHeight() - yShift + yShift2);
    g.drawString("\u03c0", getWidth() / 2, getHeight() / 2 + yShift2);
    g.drawString("2" + "\u03c0", getWidth() - (2 * xShift), getHeight() / 2 + yShift2);

    Color line = new Color (255, 0, 0);
    g.setColor(line);






    int x1 = xShift;
    int y1 = getHeight() / 2;
    int x2 = xShift;
    int y2;
    int yScale = getHeight() / 2;
    int sinInput = 0;




    while (sinInput <= 360){
        x2 += (getWidth() / 360);

        y2 = (yScale - ((int)(Math.sin(sinInput) * yScale)));


        sinInput += 1;
        g.drawLine(x1, y1, x2, y2);

        x1 = x2;
        y1 = y2;

    }










}

} }

Yes I know there are numerous things that I could tidy up or simplify, this is just a rough draft, per-se, that I will clean up once I have everything working. 是的我知道有很多东西我可以整理或简化,这只是一个粗略的草案本身,一旦我有一切工作我会清理。 I have tried multiple methods of getting the y value to properly be set, the closest I got ended up drawing a straight diagonal lines, it wasn't curved like it was supposed to be. 我已经尝试了多种方法来获得正确设置的y值,最接近的是我最终绘制了一条直的对角线,它没有像它应该的那样弯曲。 What can I do to properly set the y value so that the sin graph from [0, 2pi] will be properly drawn? 如何正确设置y值以便正确绘制[0,2pi]的sin图?

To try to clarify the issue: The problem lies in changing the y value of my drawLine function. 试图澄清问题:问题在于更改drawLine函数的y值。 Basically, what I had working wouldn't draw the sin function properly because I only could figure out to lineraly increment it. 基本上,我工作的东西不会正确地绘制sin函数,因为我只能想出以线性递增它。 It looked something like this: 它看起来像这样:

 /\
/  \  /
    \/

The ultimate question I have is: How can I make it so my y coordinate will scale with the proper y coordinate of the sin(x) graph? 我的最终问题是:我怎样才能使我的y坐标与sin(x)图的正确y坐标一起缩放? In other words, how can I make it "not-straight" and properly curved like the sin(x) graph should. 换句话说,我怎样才能使它“不直”并且像sin(x)图一样适当弯曲。 Thank you for your time I appreciate any help. 感谢您的时间,感谢您的帮助。

The latest thing I've been playing with is something that's sort of like this: The value of the y coordinate is calculated by scaling the value of the sin of the degree and manipulating it with certain factors to get the right pixel coordinate. 我一直在玩的最新的东西有点像这样:y坐标的值是通过缩放度的sin值并用某些因子操纵它来得到正确的像素坐标来计算的。 I think this is the proper way to do it, any thoughts? 我认为这是正确的方法,任何想法?

At the bottom of this page you can see what the end result should look like: http://nameless.cis.udel.edu/class_wiki/index.php/CISC181_S2015_Lab3 在此页面的底部,您可以看到最终结果应如下所示: http//nameless.cis.udel.edu/class_wiki/index.php/CISC181_S2015_Lab3

Try y= Math.sin(x)*100; 试试y = Math.sin(x)* 100; And take a scale of 并采取规模
1 =100 divisions That way you can take on the decimals. 1 = 100分区这样你可以采用小数。

Like immibis pointed out, Math.sin takes an angle in radians . 就像immibis指出的那样,Math.sin以弧度为单位 Just convert your degrees to radians while passing it to Math.sin, that should solve your problem. 只需将度数转换为弧度,同时将其传递给Math.sin,这应该可以解决您的问题。

Math.sin((sinInput * 2 * Math.PI) / 360)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM