Jump to content

How to draw sine wave in pascal graph mode?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Masonic

Masonic

    Newbie

  • Members
  • PipPip
  • 10 posts
i really need code for that, i can't find it anywhere
just simple code to draw sine wave in graph mode

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You'll need to be clearer. Is this in TurboPascal (which version), OpenPascal, Delphi (forms), Lazarus, pure Pascal (no graphics support), other?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Yes, you need to specify which pascal flavor you are using. Although I think you are using Turbo/Borland Pascal.

Actually the steps to plot the sine chart is not that difficult. Follow these steps, and you will be fine.

  • Decide which axis for angle values and which for sine values
  • Decide the resolutions for each axis. The higher the resolution is the smoother the chart will be, but will require more time and more cpu cycles (not really matter for current hardware). Remember that sine function could only returns value from 1 to -1, so prepare your resolution (I mean including the point for max and minimum values in the graph) accordingly.
  • Calculate each sine value for each angle value. For each sine value you calculated, draw a line connecting it to the previous sine value.


#4
Masonic

Masonic

    Newbie

  • Members
  • PipPip
  • 10 posts
LuthfiHakim, i understood what you are saying, but i have no idea how to write that :D
little help? im really new to graph mode, but i really need this thing...

and ok, i'm using Free Pascal

#5
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Well, you can post what you've came to, and we can see what you've missed.

Let's see if I can explain clearer.

  • Decide which axis for which value, usually horizontal axis (x) takes the angle values, and vertical axis (y) takes the sine values. Let's use this scheme for our example.
  • Decide resolution for each axis. For Y axis let's decide 100 pixels represent 1 value. Therefore -1 would be 100 pixels below the 0 point. As for the X axis, maybe we want to get full revolution (i.e. from 0 to 360) in 640 pixels wide screen, so we must settle to 1 pixels = 1 degree.
  • Calculate sine value for each angle value, and for each sine value you've calculated, draw a line connecting it to the previous sine value.

    This means you have to specify the range of angle values you want to calculate. Also you need a way to store the previous sine value (and corresponding angle) to be able to draw the line. You can use either a variable to store only the immediate previous sine/angle values or you can store all values you've calculated so far in a collection variable (could be array, linked list pointers, an instance of TList, etc).

    Storing only one immediate previous sine/angle value means you have to recalculate all the sine values each time you have to render, while storing all calculated values means you don't have to recalculate when drawing the same range of angle values, although more memory consuming but this approach preferable where your chart needs to be redrawn frequently (such as drawing on windows).

    For the sake of simplicity, let's use the storing single previous value. Also let's take 0 to 360 degrees for our range of angle values.

The psedocode would be:

type

  TCoord=

    X: Integer;

    Y: Integer;

  end;


const

  XResolution = 100;

  YResoultion = 1;


var

  P: TCoord;  // previous coordinate

  vAngle: Integer;

  vSine  : Double;

  C: TCoord;  // current coordinate

begin  

  // initiate the prior coordinate

  P.X := 0;

  P.Y := 0;


  // for each value in our range

  for vAngle := 0 to 360 do

  begin

    vSine := Sine(vAngle);

    C.X := Round(vAngle * XResolution);

    C.Y := Round(vSine * YResolution);

   

    MoveTo(P.X, P.Y);

    LineTo(C.X, C.Y);


    // store the current coord into prior coord

    // to be used in next calculation

    P := C;

  end;

end;


That's basically what you have to do. But there are 2 points left for you to do.
  • The default Sine function in Pascal takes radians value, not degrees like we used in the pseudocode. I left it for you to do the adjustment.

  • The point of origin (coordinate (0, 0)) of screen coordinates is placed exactly at topleft corner of the screen. Unlike ordinary cartesian, the positive Y axis portion is placed below the 0 point. So (0, 100) in screen coordinate means a pixel located exactly at the left border of the screen and 100 pixels below the top border.

    Therefore you must map the cartesian coordinates we get into screen coordinates before plotting the lines. You can easily done this by first mapping your cartesian point of origin (coordinate (0,0)) to a chosen coordinate in the screen, then use the map to calculate for other coordinates.

Edited by LuthfiHakim, 18 November 2010 - 11:15 PM.