Sign in to follow this  
Followers 0
zwierzak

sine graph in GDI

4 posts in this topic

#1 ·  Posted (edited)

Hello. I Want to create a sine graph in GDI. However I have some problems with it. I've made a few versions, but any of them is correct. In the first one the graph should start from the value (0,0) and in fact it starts just a little bit up from this spot. In the second one, the graph is correct, but it is fuzzy. Here are my examples, please help me.

in sine function:

($x, $y) = center of coordinate system

$d = accuracy of the graph

$j = unit (in pixels)

Version 1(inaccurate one):

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
Local $hGUI, $hGraphic, $hPen
 
; Create GUI
$hGUI = GUICreate("GDI+", 844, 375, 0, 0)
GUISetState()
; Draw line
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hPen = _GDIPlus_PenCreate()
sine(10, 200, 0.001, 50)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
; Clean up resources
_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
Func sine($x, $y, $d, $j)
_GDIPlus_GraphicsDrawLine($hGraphic, $x, $y + 2 * $j, $x, $y - 2 * $j, $hPen) ;axis y
_GDIPlus_GraphicsDrawLine($hGraphic, $x - 5, $y - $j, $x + 5, $y - $j, $hPen) ; value 1 on axis y
_GDIPlus_GraphicsDrawString($hGraphic, "1", $x + 10, $y - $j - 10)
_GDIPlus_GraphicsDrawLine($hGraphic, $x - 5, $y + $j, $x + 5, $y + $j, $hPen) ; value -1 on axis y
_GDIPlus_GraphicsDrawString($hGraphic, "-1", $x + 10, $y + $j - 10)
 
_GDIPlus_GraphicsDrawLine($hGraphic, $x, $y, $x + (844 - $x), $y, $hPen) ;axis x
_GDIPlus_GraphicsDrawLine($hGraphic, $x + 3.14159 * $j, $y - 5, $x + 3.14159 * $j, $y + 5, $hPen) ; value PI on  axis x
_GDIPlus_GraphicsDrawString($hGraphic, "PI", $x + 3.14159 * $j - 5, $y + 10)
_GDIPlus_GraphicsDrawLine($hGraphic, $x + 1.570795 * $j, $y - 5, $x + 1.570795 * $j, $y + 5, $hPen) ; value PI/2 on axis x
_GDIPlus_GraphicsDrawString($hGraphic, "PI / 2", $x + 1.570795 * $j - 10, $y + 10)
 
Local $x1 = $x / $j, $y1 = $y / $j, $x2 = 0, $y2 = 0
Do
   $x2 = ($x1 + $d)
   $y2 = $y/$j - Sin($x2)
   _GDIPlus_GraphicsDrawLine($hGraphic,  $x1 * $j, $y1 * $j , $x2 * $j, $y2 * $j, $hPen)
   $y1 = $y2
   $x1 = $x2
  Until $x2 >= 6.29 * $j ;2 pi
EndFunc   ;==>sinus

Version 2(fuzzy one):

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
Local $hGUI, $hGraphic, $hPen
 
; Create GUI
$hGUI = GUICreate("GDI+", 844, 375, 0, 0)
GUISetState()
; Draw line
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hPen = _GDIPlus_PenCreate()
sine(10, 200, 0.001, 50)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
; Clean up resources
_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
Func sine($x, $y, $d, $j)
 
_GDIPlus_GraphicsDrawLine($hGraphic, $x, $y + 2 * $j, $x, $y - 2 * $j, $hPen) ;axis y
_GDIPlus_GraphicsDrawLine($hGraphic, $x - 5, $y - $j, $x + 5, $y - $j, $hPen) ; value 1 on axis y
_GDIPlus_GraphicsDrawString($hGraphic, "1", $x + 10, $y - $j - 10)
_GDIPlus_GraphicsDrawLine($hGraphic, $x - 5, $y + $j, $x + 5, $y + $j, $hPen) ; value -1 on axis y
_GDIPlus_GraphicsDrawString($hGraphic, "-1", $x + 10, $y + $j - 10)
 
_GDIPlus_GraphicsDrawLine($hGraphic, $x, $y, $x + (844 - $x), $y, $hPen) ;axis x
_GDIPlus_GraphicsDrawLine($hGraphic, $x + 3.14159 * $j, $y - 5, $x + 3.14159 * $j, $y + 5, $hPen) ; value PI on  axis x
_GDIPlus_GraphicsDrawString($hGraphic, "PI", $x + 3.14159 * $j - 5, $y + 10)
_GDIPlus_GraphicsDrawLine($hGraphic, $x + 1.570795 * $j, $y - 5, $x + 1.570795 * $j, $y + 5, $hPen) ; value PI/2 on axis x
_GDIPlus_GraphicsDrawString($hGraphic, "PI / 2", $x + 1.570795 * $j - 10, $y + 10)
$i = 0.0
Do
  $i = $i + $d
  $x2 = $x + $i * $j
  $gx2 = Round($x2)
  $y2 = $y - (Sin($i) * $j)
  $gy2 = Round($y2)
  _GDIPlus_GraphicsDrawLine($hGraphic, $gx2 * $j, $gy2 * $j, $gx2, $gy2, $hPen)
  ConsoleWrite($gx2 * $j & @CRLF)
  ConsoleWrite($gy2 * $j & @CRLF)
  ConsoleWrite("=======" & @CRLF)
Until $i >= 6.29 * 2
EndFunc   ;==>sinus
Edited by zwierzak

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this:

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
Local $hGUI, $hGraphic, $hPen

; Create GUI
$hGUI = GUICreate("GDI+", 844, 375, 0, 0)
GUISetState()
; Draw line
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
_GDIPlus_GraphicsClear($hGraphic, 0xFFF0F0F0)
$hPen = _GDIPlus_PenCreate(0xFF000000, 1)
sine(10, 200, 0.01, 100)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
; Clean up resources
_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()

Func sine($x, $y, $d, $j)
Local $pi = ACos(-1)
Local Const $2pi = 2 * $pi
Local Const $pi2 = $pi / 2
Local Const $pi32 = 3  /  2 * $pi
_GDIPlus_GraphicsDrawLine($hGraphic, $x, $y + 2 * $j, $x, $y - 2 * $j, $hPen) ;axis y
_GDIPlus_GraphicsDrawLine($hGraphic, $x - 5, $y - $j, $x + 5, $y - $j, $hPen) ; value 1 on axis y
_GDIPlus_GraphicsDrawString($hGraphic, "1", $x + 10, $y - $j - 10)
_GDIPlus_GraphicsDrawLine($hGraphic, $x - 5, $y + $j, $x + 5, $y + $j, $hPen) ; value -1 on axis y
_GDIPlus_GraphicsDrawString($hGraphic, "-1", $x + 10, $y + $j - 10)

_GDIPlus_GraphicsDrawLine($hGraphic, $x, $y, $x + (844 - $x), $y, $hPen) ;axis x
_GDIPlus_GraphicsDrawLine($hGraphic, $x + $pi * $j, $y - 5, $x + $pi * $j, $y + 5, $hPen) ; value PI on  axis x
_GDIPlus_GraphicsDrawLine($hGraphic, $x + $pi2 * $j, $y - 5, $x + $pi2 * $j, $y + 5, $hPen) ; value PI/2 on axis x
_GDIPlus_GraphicsDrawLine($hGraphic, $x + $pi32 * $j, $y - 5, $x + $pi32 * $j, $y + 5, $hPen) ; value 3/2PI on axis x
_GDIPlus_GraphicsDrawLine($hGraphic, $x + $2pi * $j, $y - 5, $x + $2pi * $j, $y + 5, $hPen) ; value 2PI on axis x
_GDIPlus_GraphicsDrawString($hGraphic, "PI / 2", $x + $pi2 * $j - 10, $y + 10)
_GDIPlus_GraphicsDrawString($hGraphic, "PI", $x + $pi * $j - 5, $y + 10)
_GDIPlus_GraphicsDrawString($hGraphic, "3 / 4 PI", $x + $pi32 * $j - 10, $y + 10)
_GDIPlus_GraphicsDrawString($hGraphic, "2 PI", $x + $2pi * $j - 5, $y + 10)


Local $x1 = $x / $j, $y1 = $y / $j, $x2 = 0, $y2 = 0
Local $l = 0
Do
    _GDIPlus_PenSetColor($hPen, 0xFF000000)
    _GDIPlus_GraphicsDrawRect($hGraphic, $x + ($l) * $j, $y + Sin(-$l) * $j, 1, 1, $hPen)
    _GDIPlus_PenSetColor($hPen, 0xFF0000FF)
    _GDIPlus_GraphicsDrawRect($hGraphic, $x + ($l) * $j, $y + -Cos($l) * $j, 1, 1, $hPen)

    $l += $d
  Until $l > $2pi
EndFunc   ;==>sinus

Br,

UEZ

Edit: added also cosinus

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

WOW it is great! I wonder if you could explain me how does it work exactly? I couldn't notice mistake in my versions ;/

Share this post


Link to post
Share on other sites

Look here: http://en.wikipedia.org/wiki/Sine

Have in mind the 0° is not on top but on the right side (90°) in our system.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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  
Followers 0