Jump to content
Sign in to follow this  
cryton2707

help with a sin wave. please !

Recommended Posts

Im wanting to create a sin wave the width of the screen..

I did find some C code my maths aint up to the job of properly understanding the code..

can any one help me convert it ..

Thanks

CODE
void draw_sine ()

{

int length = 50;

fixed x, y;

fixed angle = 0;

fixed angle_stepsize = itofix (5);

while (fixtoi(angle) < 256)

{

// the angle is plotted along the x-axis

x = angle;

// the sine function is plotted along the y-axis

y = length * fsin (angle);

putpixel (screen,

fixtoi (x), fixtoi (y) + SCREEN_H / 2,

makecol (255, 255, 255));

angle += angle_stepsize;

}

}

whats this fixtoi above and itofix used for above..

Thanks

Chris


Welcome to the internet :) where men are men! Women are men! and 16 year old women are FBI agents!

Share this post


Link to post
Share on other sites

Hmm...

A sin wave is drawn on a unit graph, usually. For a single iteration, using the whole screen, transpose the existing coordinates to a unit graph like so:

0, 0 = $x = @DesktopWidth / 2, $y = @DesktopHeight/2

1, -1 = @DesktopWidth, @DesktopHeight

-1, 1 = 0, 0

1, 1 = @DesktopWidth, 0

-1, -1 = 0, @DesktopHeight


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

Hey! I was SUPER bored... I should have been studying though...

warning!: Selecting the highest accuracy will cause 100% CPU load and it will take it a while ;) (At least on an average comp..)

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Main = GUICreate("Super Trigonométrico", 228, 364)
GUICtrlCreateLabel("more", 184, 156, 27, 17)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlCreateLabel("Please select the function you wish to draw", 8, 24, 208, 17)
$Sin = GUICtrlCreateRadio("Sin", 32, 48, 41, 17)
GUICtrlSetState(-1,$GUI_CHECKED)
$Cos = GUICtrlCreateRadio("Cos", 88, 48, 49, 17)
$Tan = GUICtrlCreateRadio("Tan", 144, 48, 65, 17)

GUICtrlCreateLabel("Select accuracy", 16, 104, 81, 17)
$Acc = GUICtrlCreateSlider  (16, 128, 185, 17)
GUICtrlSetLimit(-1,4,1)
GUICtrlSetData(-1,2)
GUICtrlCreateGroup("", 0, 0, 225, 89)
GUICtrlCreateGroup("", 0, 88, 225, 89)
GUICtrlCreateGroup("", 0, 176, 225, 113)

GUICtrlCreateLabel("less", 8, 156, 22, 17)
GUICtrlSetColor(-1, 0xFFFF00)
GUICtrlCreateLabel("Recommended!", 72, 158, 79, 17)
GUICtrlSetColor(-1, 0x008080)
GUICtrlCreateLabel("Select axis scales", 16, 192, 88, 17)
$Xscale = GUICtrlCreateSlider(40, 216, 137, 17)
GUICtrlSetLimit(-1,100,1)
GUICtrlSetData(-1,50)
$Yscale = GUICtrlCreateSlider(40, 240, 137, 17)
GUICtrlSetLimit(-1,100,1)
GUICtrlSetData(-1,50)
GUICtrlCreateLabel("X", 24, 216, 11, 17)
GUICtrlCreateLabel("Y", 24, 240, 11, 17)

$draw = GUICtrlCreateButton("Draw!", 32, 312, 161, 33, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
                Exit
        Case $draw
            If GUICtrlRead($Sin)=$GUI_CHECKED Then
            draw("sin")
            Else
                If GUICtrlRead($Cos)=$GUI_CHECKED Then
                    draw("Cos")
                Else
                    draw("Tan")
                EndIf
            EndIf
    EndSwitch
WEnd

Func draw($sFunc)
    Local $GrAcc=1
    $Accuracy=GUICtrlRead($Acc)
    Switch $Accuracy
        Case 1
            $GrAcc=1
        Case 2
            $GrAcc=0.5
        Case 3
            $GrAcc=0.05
        case 4
            $GrAcc=0.005
    EndSwitch
    $scaleX=GUICtrlRead($Xscale)
    $scaleY=GUICtrlRead($Yscale)
    
    ;Create Child Gui
    $child=GUICreate("",@DesktopWidth,@DesktopHeight)
    GUISetBkColor(0xFFFFFF,$child)
    $exit=GUICtrlCreateButton("Exit",@DesktopWidth-150,@DesktopHeight-50,100)
    $g = GUICtrlCreateGraphic(1,1,@DesktopWidth-1,@DesktopHeight-1)
    
    SplashTextOn("","Drawing...",150,25,-1,-1,1)
    If $sFunc="sin" Then
        For $i=1 to @DesktopWidth Step $GrAcc
            $y0=(Sin($i/$scaleX)*$scaleY)+(@DesktopHeight/2)
            GUICtrlSetGraphic($g,$GUI_GR_DOT,$i,$y0)
        Next
    EndIf
    If $sFunc="cos" Then
        For $i=1 to @DesktopWidth Step $GrAcc
            $y0=(Cos($i/$scaleX)*$scaleY)+(@DesktopHeight/2)
            GUICtrlSetGraphic($g,$GUI_GR_DOT,$i,$y0)
        Next
    EndIf
    If $sFunc="tan" Then
        For $i=1 to @DesktopWidth Step $GrAcc
            $y0=(Tan($i/$scaleX)*$scaleY)+(@DesktopHeight/2)
            GUICtrlSetGraphic($g,$GUI_GR_DOT,$i,$y0)
        Next
    EndIf
    SplashOff()
    GUISetState()
    While 1
        $msg=GUIGetMsg()
        Select
            Case $msg=$GUI_EVENT_CLOSE
                GUIDelete($child)
                ExitLoop
            Case $msg=$exit
                GUidelete($child)
                ExitLoop
        EndSelect
    WEnd
    
EndFunc

I thought of using the dll GDI32.dll so the graphic would be actually on the screen! But for some reasong it fucked up and I had to restart like 3 times... :)

Edited by Nahuel

Share this post


Link to post
Share on other sites

Hey! I was SUPER bored... I should have been studying though...

warning!: Selecting the highest accuracy will cause 100% CPU load and it will take it a while ;) (At least on an average comp..)

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Main = GUICreate("Super Trigonométrico", 228, 364)
GUICtrlCreateLabel("more", 184, 156, 27, 17)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlCreateLabel("Please select the function you wish to draw", 8, 24, 208, 17)
$Sin = GUICtrlCreateRadio("Sin", 32, 48, 41, 17)
GUICtrlSetState(-1,$GUI_CHECKED)
$Cos = GUICtrlCreateRadio("Cos", 88, 48, 49, 17)
$Tan = GUICtrlCreateRadio("Tan", 144, 48, 65, 17)

GUICtrlCreateLabel("Select accuracy", 16, 104, 81, 17)
$Acc = GUICtrlCreateSlider  (16, 128, 185, 17)
GUICtrlSetLimit(-1,4,1)
GUICtrlSetData(-1,2)
GUICtrlCreateGroup("", 0, 0, 225, 89)
GUICtrlCreateGroup("", 0, 88, 225, 89)
GUICtrlCreateGroup("", 0, 176, 225, 113)

GUICtrlCreateLabel("less", 8, 156, 22, 17)
GUICtrlSetColor(-1, 0xFFFF00)
GUICtrlCreateLabel("Recommended!", 72, 158, 79, 17)
GUICtrlSetColor(-1, 0x008080)
GUICtrlCreateLabel("Select axis scales", 16, 192, 88, 17)
$Xscale = GUICtrlCreateSlider(40, 216, 137, 17)
GUICtrlSetLimit(-1,100,1)
GUICtrlSetData(-1,50)
$Yscale = GUICtrlCreateSlider(40, 240, 137, 17)
GUICtrlSetLimit(-1,100,1)
GUICtrlSetData(-1,50)
GUICtrlCreateLabel("X", 24, 216, 11, 17)
GUICtrlCreateLabel("Y", 24, 240, 11, 17)

$draw = GUICtrlCreateButton("Draw!", 32, 312, 161, 33, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
                Exit
        Case $draw
            If GUICtrlRead($Sin)=$GUI_CHECKED Then
            draw("sin")
            Else
                If GUICtrlRead($Cos)=$GUI_CHECKED Then
                    draw("Cos")
                Else
                    draw("Tan")
                EndIf
            EndIf
    EndSwitch
WEnd

Func draw($sFunc)
    Local $GrAcc=1
    $Accuracy=GUICtrlRead($Acc)
    Switch $Accuracy
        Case 1
            $GrAcc=1
        Case 2
            $GrAcc=0.5
        Case 3
            $GrAcc=0.05
        case 4
            $GrAcc=0.005
    EndSwitch
    $scaleX=GUICtrlRead($Xscale)
    $scaleY=GUICtrlRead($Yscale)
    
    ;Create Child Gui
    $child=GUICreate("",@DesktopWidth,@DesktopHeight)
    GUISetBkColor(0xFFFFFF,$child)
    $exit=GUICtrlCreateButton("Exit",@DesktopWidth-150,@DesktopHeight-50,100)
    $g = GUICtrlCreateGraphic(1,1,@DesktopWidth-1,@DesktopHeight-1)
    
    SplashTextOn("","Drawing...",150,25,-1,-1,1)
    If $sFunc="sin" Then
        For $i=1 to @DesktopWidth Step $GrAcc
            $y0=(Sin($i/$scaleX)*$scaleY)+(@DesktopHeight/2)
            GUICtrlSetGraphic($g,$GUI_GR_DOT,$i,$y0)
        Next
    EndIf
    If $sFunc="cos" Then
        For $i=1 to @DesktopWidth Step $GrAcc
            $y0=(Cos($i/$scaleX)*$scaleY)+(@DesktopHeight/2)
            GUICtrlSetGraphic($g,$GUI_GR_DOT,$i,$y0)
        Next
    EndIf
    If $sFunc="tan" Then
        For $i=1 to @DesktopWidth Step $GrAcc
            $y0=(Tan($i/$scaleX)*$scaleY)+(@DesktopHeight/2)
            GUICtrlSetGraphic($g,$GUI_GR_DOT,$i,$y0)
        Next
    EndIf
    SplashOff()
    GUISetState()
    While 1
        $msg=GUIGetMsg()
        Select
            Case $msg=$GUI_EVENT_CLOSE
                GUIDelete($child)
                ExitLoop
            Case $msg=$exit
                GUidelete($child)
                ExitLoop
        EndSelect
    WEnd
    
EndFunc

I thought of using the dll GDI32.dll so the graphic would be actually on the screen! But for some reasong it fucked up and I had to restart like 3 times... :)

Language mister :P Thats pretty cool. I like how it looks if i go TAN, More, X & Y = lowest. Its like. :P

Share this post


Link to post
Share on other sites

Language mister ;) Thats pretty cool. I like how it looks if i go TAN, More, X & Y = lowest. Its like. :)

haha, awesome!

Try:

  • Tan
  • Accuracy:3rd position
  • X=Lowest
  • Y=Highest
Edited by Nahuel

Share this post


Link to post
Share on other sites

$size = WinGetPos("Program Manager")

$hdc = DllCall("user32.dll","int","GetDC","hwnd",0)

$x = 0
while $x < $size[2]
    DllCall("gdi32.dll","int","SetPixel","int",$hdc[0],"int",$x,"int",getNormalizedSine($x, ($size[3]/2), $size[2]),"int",0xFF00FF)
    $x += 1
WEnd
DllCall("user32.dll","int","ReleaseDC","hwnd",0,"int",$hdc[0])

Func getNormalizedSine($xxx, $halfY, $maxX)
    $piDouble = 2 * 3.14
    $factor = $piDouble / $maxX
    Return Int(Sin($xxx * $factor) * $halfY + $halfY)
EndFunc

Share this post


Link to post
Share on other sites

$size = WinGetPos("Program Manager")

$hdc = DllCall("user32.dll","int","GetDC","hwnd",0)

$x = 0
while $x < $size[2]
    DllCall("gdi32.dll","int","SetPixel","int",$hdc[0],"int",$x,"int",getNormalizedSine($x, ($size[3]/2), $size[2]),"int",0xFF00FF)
    $x += 1
WEnd
DllCall("user32.dll","int","ReleaseDC","hwnd",0,"int",$hdc[0])

Func getNormalizedSine($xxx, $halfY, $maxX)
    $piDouble = 2 * 3.14
    $factor = $piDouble / $maxX
    Return Int(Sin($xxx * $factor) * $halfY + $halfY)
EndFunc
Oooohhhh that's so far away from what I had done, haha.. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...