gpt4 book ai didi

c - 为什么此代码将正弦曲线倒置打印

转载 作者:可可西里 更新时间:2023-11-01 09:24:16 24 4
gpt4 key购买 nike

生成的 y 值似乎是正确的。请参阅 printf 打印输出,它打印出递增的 y 值。但是当发送到 SetPixel 函数时,它似乎打印出正弦曲线,就像乘以 -1 一样???

怎么了?

#include <windows.h>
//#include <stdio.h>
#include <math.h>

int main()
{
HWND console = GetConsoleWindow();
HDC dc = GetDC(console);
int pixel =0;
COLORREF C1= RGB(255,0,0); /* red */
for (double i = 0; i< 6.3; i+=0.05)
{
SetPixel(dc,pixel,(int)(100+50*sin(i)),C1);
/*printf("%d ", (int)(100+50*sin(i))); // prints numbers as expected, eg 100 102 104 107 109 112 etc */
pixel+=1;
}
ReleaseDC(console, dc);
return 0;
}

反馈后

由于 Windows 坐标系从左上角开始 (0,0),您可以像这样更改 sin 函数的符号:

SetPixel(dc,pixel,(int)(100+50*-sin(i)),C1);

有效。

最佳答案

坐标系不是您所期望的:y == 0 是屏幕的顶部,而不是底部。

参见 Windows Coordinate System :

The x-coordinates increase to the right; y-coordinates increase from top to bottom.

following很好地说明了它(它谈论的是 Java 坐标,但 Windows 坐标是相同的):

Coordinate systems

解决这个问题的一个简单方法是翻转 sin() 的符号:

    SetPixel(dc,pixel,(int)(100-50*sin(i)),C1);

关于c - 为什么此代码将正弦曲线倒置打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28009828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com