Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Draw a sine wave in a Gui using math.sin()?

Asked by 6 years ago
Edited 6 years ago

Is it possible to draw a sine wave in Roblox Lua inside a Gui without using Decals? Here's the setup:

num = 50
f = 20
fs = 100
y = math.sin(2 * math.pi * f * num / fs)


For reference, I have a C++ program that displays it in a window, like this (sorry, I used the Lua encoder):

#include <Windows.h>
#include <math.h>

#define screen_width 800
#define screen_height 600
#define pi 3.14159

COLORREF blue = RGB(0, 0, 255);

double _freq = 50;
int _sTime = 5, _amp = 100;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void wavefunc(HWND, HDC);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;
    HDC hDC;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = (HICON)LoadImage(NULL, "C:\\Visual Studio 2010\\Icons\\Smiley.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
    wc.lpszClassName = "SineWave";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          "SineWave", "Wave",
                          WS_SYSMENU,
                          0, 45,
                          screen_width, screen_height,
                          NULL, NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);
    hDC = GetDC(hWnd);

    MSG msg;

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {DispatchMessage(&msg);}

        if(msg.message == WM_QUIT)
            break;

        wavefunc(hWnd, hDC);
    }

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } break;
        case WM_KEYDOWN:
        {
            switch (wParam)
            {
            case VK_NUMPAD4:
                _freq--;
                if(_freq < 1.0)
                    _freq = 1;
                break;
            case VK_NUMPAD6:
                _freq++;
                break;
            case VK_NUMPAD8:
                _amp++;
                if(_amp > 200)
                    _amp = 200;
                break;
            case VK_NUMPAD2:
                _amp--;
                if(_amp < 0)
                    _amp = 0;
                break;
            case VK_SUBTRACT:
                _sTime++;
                if(_sTime > 50)
                    _sTime = 50;
                break;
            case VK_ADD:
                _sTime--;
                if(_sTime < 0)
                    _sTime = 0;
                break;
            }
            return 0;
        } break;
     }
     return DefWindowProc(hWnd, message, wParam, lParam);
 }

void wavefunc(HWND hWnd, HDC hDC)
{
    double full = 2 * pi * _freq;
    static double _x = 0;
    int _y = (sin(_x/_freq)*_amp) + 300;
    if (_x >= full)
        _x -= full;
    SetPixel(hDC, 600, _y, blue);
    ScrollWindow(hWnd, -1, 0, NULL, NULL);
    Sleep(_sTime);
    _x++;
}

But how would I do that in Roblox Lua?

1 answer

Log in to vote
0
Answered by 6 years ago

I'm not sure if the calculation is right (I'm not that boy who knows everything about math. :D But, I can tell you that you could maybe do instance.new and create a textlabel, then set the size to: Udim2.new(0,1,0,1) (The smallest that I recommend you.)

And set the position to another Udim2 value. However, if you want to do this, take note of the following things: - You need a lot of points of your wave, you could auto calculate them - There will be a lot of TextLabels, so you can expect a lot of lag.

I think this is the only option if you do not want to use decals.

0
I want a response from somebody who has actually taken Calculus AP, thank you. DeceptiveCaster 3761 — 6y
0
Ok. If I would know what it was. :) marketmanager1 52 — 6y
Ad

Answer this question