Board index » delphi » Additional problem, with the graph im trying to get workin

Additional problem, with the graph im trying to get workin

Attached is my current code, the function is set to 10x^3+3x^2+2x+5 (a
simple cubic eqn) and it draws it fine, but to the left and right of the
curve, there are lines which arent suppost to be there. can anyone suggest
why??

I was thinking that when the y-vales go off the axis, it will put them on
the next line, i have tried sticking an IF statement in the loop, just above
the lineto statement, but with no luck, the lines still are there. (In the
code attached, i have removed the IF)

any help would be great. thanks

-Daniel Yazbek

Quote
"D Yazbek" <dyaz...@optushome.com.au> wrote in message

news:%tOb5.1166$gg.3416@news1.rivrw1.nsw.optushome.com.au...
Quote

> From: "D Yazbek" <dyaz...@optushome.com.au>
> Subject: Working with pixels
> Date: Friday, 14 July 2000 5:15 PM

> I am using these procedures, to allow me to plot pixels: I did not design
> them, i took them from a web page. Now, i have managed to use these
> procedures to generate an x:y axis, like maths, where curves are drawn.
now,
> how do i use the resolution the axis is drawn in, to put little labels on
> the ends of each axis, eg(----> x) for the x-axis?

> I am not a pro at pascal, im in high school. so please keep the jargon at
a
> high-school level if possible, thanks.

> PROCEDURE InitGraph;
> BEGIN
>   ASM
>     mov AX, 0013h    { Function 0, mode 13h }
>     int 10h
>   END;
>   Screen := PTR($A000, 0);   { Set up the screen buffer pointer }
> END;

> PROCEDURE CloseGraph; ASSEMBLER;
> ASM
>   mov AX, 0003h      { Function 0, Mode 3}
>   int 10h
> END;

> PROCEDURE SetPixel(X, Y : INTEGER; Color : BYTE);
> VAR
>   Address : WORD;
> BEGIN
>   Address := Y*320+X;
>   Screen^[Address] := Color;
> END;

 

Re:Additional problem, with the graph im trying to get workin


Re:Additional problem, with the graph im trying to get workin


Ignore the code below, i have deleated it, my new code is this: also, with
the y-limiter IF statement in the loop, it distorts any part of the graph
under the x-axis.

USES crt,graph;

CONST gridx=10;
      gridy=10;

FUNCTION f(x:real):real;
BEGIN
  f:=(10*x*x*x)*3*sqr(x)+2*x+5;
END;

VAR gd,gm,mx,my,i:integer;
    x,y:real;

BEGIN
  gd:=VGA;        {BGI settings for 640*480}
  gm:=VGAhi;
  initgraph(gd,gm,'c:\tp\bgi');
  setcolor(12);  {red for the axis}
  moveto(319,0);  {sets curser to co-ord (319,0) which is top of y-axis}
  lineto(319,479);{draws the Y-Axis by drawing a line to (319,479) which is
bottom of y-axis}
  moveto(0,239);{sets curser to co-ord (0,239) which is left side of x-axis}
  lineto(639,239);{draws the x-axis by drawing a line to (639,479) which is
the right side of x-axis}
  mx:=getmaxX DIV 2;   {finds center x co-ord}
  my:=getmaxY DIV 2;   {finds center y co-ord}
  x:=(-my*gridx)/my;   {sets starting x point}
  y:=(f(x)*my)/gridy;  {sets starting y point}
  moveto(mx-my,my-trunc(y));
  setcolor(15);
  FOR i:=-my TO my DO {mx,my = (319,239) in 640*460}
    BEGIN
      x:=(i*gridx)/my;   {sets the next x co-ord to plot}
      y:=(f(x)*my)/gridy;{sets the next y co-ord to plot}
      lineto(mx+i,my-trunc(y));{joins the x coord, with the y coord with a
line}
    END;
  readkey;
end.

Quote
"D Yazbek" <dyaz...@optushome.com.au> wrote in message

news:MWSc5.1454$gg.4036@news1.rivrw1.nsw.optushome.com.au...
Quote
> Attached is my current code, the function is set to 10x^3+3x^2+2x+5 (a
> simple cubic eqn) and it draws it fine, but to the left and right of the
> curve, there are lines which arent suppost to be there. can anyone suggest
> why??

> I was thinking that when the y-vales go off the axis, it will put them on
> the next line, i have tried sticking an IF statement in the loop, just
above
> the lineto statement, but with no luck, the lines still are there. (In the
> code attached, i have removed the IF)

> any help would be great. thanks

> -Daniel Yazbek

> "D Yazbek" <dyaz...@optushome.com.au> wrote in message
> news:%tOb5.1166$gg.3416@news1.rivrw1.nsw.optushome.com.au...

> > From: "D Yazbek" <dyaz...@optushome.com.au>
> > Subject: Working with pixels
> > Date: Friday, 14 July 2000 5:15 PM

> > I am using these procedures, to allow me to plot pixels: I did not
design
> > them, i took them from a web page. Now, i have managed to use these
> > procedures to generate an x:y axis, like maths, where curves are drawn.
> now,
> > how do i use the resolution the axis is drawn in, to put little labels
on
> > the ends of each axis, eg(----> x) for the x-axis?

> > I am not a pro at pascal, im in high school. so please keep the jargon
at
> a
> > high-school level if possible, thanks.

> > PROCEDURE InitGraph;
> > BEGIN
> >   ASM
> >     mov AX, 0013h    { Function 0, mode 13h }
> >     int 10h
> >   END;
> >   Screen := PTR($A000, 0);   { Set up the screen buffer pointer }
> > END;

> > PROCEDURE CloseGraph; ASSEMBLER;
> > ASM
> >   mov AX, 0003h      { Function 0, Mode 3}
> >   int 10h
> > END;

> > PROCEDURE SetPixel(X, Y : INTEGER; Color : BYTE);
> > VAR
> >   Address : WORD;
> > BEGIN
> >   Address := Y*320+X;
> >   Screen^[Address] := Color;
> > END;

Re:Additional problem, with the graph im trying to get workin


sorry, in the function decleration, the * infront of the 3 is ment to be a
+, but still, the same problem exisits.

Quote
"D Yazbek" <dyaz...@optushome.com.au> wrote in message

news:9ZSc5.1455$gg.4032@news1.rivrw1.nsw.optushome.com.au...
Quote
> Ignore the code below, i have deleated it, my new code is this: also, with
> the y-limiter IF statement in the loop, it distorts any part of the graph
> under the x-axis.

> USES crt,graph;

> CONST gridx=10;
>       gridy=10;

> FUNCTION f(x:real):real;
> BEGIN
>   f:=(10*x*x*x)+3*sqr(x)+2*x+5;
> END;

> VAR gd,gm,mx,my,i:integer;
>     x,y:real;

> BEGIN
>   gd:=VGA;        {BGI settings for 640*480}
>   gm:=VGAhi;
>   initgraph(gd,gm,'c:\tp\bgi');
>   setcolor(12);  {red for the axis}
>   moveto(319,0);  {sets curser to co-ord (319,0) which is top of y-axis}
>   lineto(319,479);{draws the Y-Axis by drawing a line to (319,479) which
is
> bottom of y-axis}
>   moveto(0,239);{sets curser to co-ord (0,239) which is left side of
x-axis}
>   lineto(639,239);{draws the x-axis by drawing a line to (639,479) which
is
> the right side of x-axis}
>   mx:=getmaxX DIV 2;   {finds center x co-ord}
>   my:=getmaxY DIV 2;   {finds center y co-ord}
>   x:=(-my*gridx)/my;   {sets starting x point}
>   y:=(f(x)*my)/gridy;  {sets starting y point}
>   moveto(mx-my,my-trunc(y));
>   setcolor(15);
>   FOR i:=-my TO my DO {mx,my = (319,239) in 640*460}
>     BEGIN
>       x:=(i*gridx)/my;   {sets the next x co-ord to plot}
>       y:=(f(x)*my)/gridy;{sets the next y co-ord to plot}
>       lineto(mx+i,my-trunc(y));{joins the x coord, with the y coord with a
> line}
>     END;
>   readkey;
> end.

> "D Yazbek" <dyaz...@optushome.com.au> wrote in message
> news:MWSc5.1454$gg.4036@news1.rivrw1.nsw.optushome.com.au...
> > Attached is my current code, the function is set to 10x^3+3x^2+2x+5 (a
> > simple cubic eqn) and it draws it fine, but to the left and right of the
> > curve, there are lines which arent suppost to be there. can anyone
suggest
> > why??

> > I was thinking that when the y-vales go off the axis, it will put them
on
> > the next line, i have tried sticking an IF statement in the loop, just
> above
> > the lineto statement, but with no luck, the lines still are there. (In
the
> > code attached, i have removed the IF)

> > any help would be great. thanks

> > -Daniel Yazbek

> > "D Yazbek" <dyaz...@optushome.com.au> wrote in message
> > news:%tOb5.1166$gg.3416@news1.rivrw1.nsw.optushome.com.au...

> > > From: "D Yazbek" <dyaz...@optushome.com.au>
> > > Subject: Working with pixels
> > > Date: Friday, 14 July 2000 5:15 PM

> > > I am using these procedures, to allow me to plot pixels: I did not
> design
> > > them, i took them from a web page. Now, i have managed to use these
> > > procedures to generate an x:y axis, like maths, where curves are
drawn.
> > now,
> > > how do i use the resolution the axis is drawn in, to put little labels
> on
> > > the ends of each axis, eg(----> x) for the x-axis?

> > > I am not a pro at pascal, im in high school. so please keep the jargon
> at
> > a
> > > high-school level if possible, thanks.

> > > PROCEDURE InitGraph;
> > > BEGIN
> > >   ASM
> > >     mov AX, 0013h    { Function 0, mode 13h }
> > >     int 10h
> > >   END;
> > >   Screen := PTR($A000, 0);   { Set up the screen buffer pointer }
> > > END;

> > > PROCEDURE CloseGraph; ASSEMBLER;
> > > ASM
> > >   mov AX, 0003h      { Function 0, Mode 3}
> > >   int 10h
> > > END;

> > > PROCEDURE SetPixel(X, Y : INTEGER; Color : BYTE);
> > > VAR
> > >   Address : WORD;
> > > BEGIN
> > >   Address := Y*320+X;
> > >   Screen^[Address] := Color;
> > > END;

Re:Additional problem, with the graph im trying to get workin


Re:Additional problem, with the graph im trying to get workin


Quote
D Yazbek <dyaz...@optushome.com.au> wrote:
> Attached is my current code, the function is set to 10x^3+3x^2+2x+5 (a
> simple cubic eqn) and it draws it fine, but to the left and right of the
> curve, there are lines which arent suppost to be there. can anyone suggest
> why??

Please *do not* attach files to your posts. I and many others will not see
them! Instead, post the source code as part of your message, or if it is
very long, put it on a web page and post a link.

If we cannot see your code, it is unlikely that we will be able to help you
solve your problem, unless it's RTE200.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Re:Additional problem, with the graph im trying to get workin


Re:Additional problem, with the graph im trying to get workin


Quote
D Yazbek <dyaz...@optushome.com.au> wrote:

[...]

Quote
> CONST gridx=10;
>       gridy=10;
> FUNCTION f(x:real):real;
> BEGIN
>   f:=(10*x*x*x)+3*sqr(x)+2*x+5;
> END;
> VAR gd,gm,mx,my,i:integer;
>     x,y:real;
> BEGIN

[init graph and draw axes]

Quote
>   mx:=getmaxX DIV 2;   {finds center x co-ord}
>   my:=getmaxY DIV 2;   {finds center y co-ord}

It would be best to have this *before* you draw the axes, and use these
numbers for drawing the axes. That way if you decide you want to use a
different mode you don't have to go and change all your hardcoded
parameters.

Quote
>   x:=(-my*gridx)/my;   {sets starting x point}

This is clearly wrong, since its effect is to set x to -gridx, i.e. -10.
Assuming you want to set x to the first value appearing on screen, it
should be either "x := -mx / gridx" or "x := -mx*gridx" - you're using gridx
and gridy in inconsistent ways (more on that later).

Quote
>   y:=(f(x)*my)/gridy;  {sets starting y point}

You should use x and y in the same context. If you are using x as an
argument to f, you are considering x to be the real (mathematical) value of
the point you want, so you should just have y as f(x). This formula also
doesn't seem to make much sense. You are effectively multiplying y by 23.9.

Quote
>   moveto(mx-my,my-trunc(y));

I don't think you want mx-my here.

Quote
>   setcolor(15);
>   FOR i:=-my TO my DO {mx,my = (319,239) in 640*460}

This should be "for i := -mx to mx do..."

Quote
>     BEGIN
>       x:=(i*gridx)/my;   {sets the next x co-ord to plot}
>       y:=(f(x)*my)/gridy;{sets the next y co-ord to plot}
>       lineto(mx+i,my-trunc(y));{joins the x coord, with the y coord with a
> line}
>     END;

Same problems here...

You need to work out how to convert real coordinates to screen coordinates,
and vice versa. Also, what are you intending "gridx" and "gridy" for? As
you're using it, the x axis has 10 pixels per unit, while the y axis has 0.1
pixels per unit (i.e 10 units per pixel). This is confusing. Do you actually
want a domain of (-32, 32) and a range of (-2400, 2400)? [This may be
reasonable for the problem...] If so, use gridx = 10 and gridy = 0.1 or the
reverse, and adjust your formulae appropriately.

Your main loop should look something like this:

  for i := -mx to mx do
    begin
      x := i / gridx;
      y := f(x);
      screenx := mx + i; {simpler than round(mx + x * gridx)}
      screeny := round(my - y * gridy);
      lineto(screenx, screeny);
    end;

You can get your first coordinate by duplicating this for i = -mx, i.e.
(with some simplifications):

  screenx := 0;
  y := f(-mx / gridx);
  screeny := round(my - y * gridy);
  moveto(screenx, screeny);

Here I've assumed that gridx and gridy represent the number of pixels per
unit, so if you make them bigger you'll be able to fit less in. I imagine
you'd want a pretty small domain for x and a somewhat larger one for y,
perhaps something like (-5, 5) for x and (-100, 100) for y. You could thus
try gridx ~ 60 and gridy ~ 2.5 - adjust these for your own desires. It's
also a good idea to include some sanity checking (i.e. make sure your points
will be on the screen, although in most cases the graph unit will take care
of this for you; also, you might want to check that a reasonable proportion
of the graph will actually apear on screen, and if not print a warning or
adjust the grid spacing to suit).

Anyway, this should clear up your problems; see how you go.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Re:Additional problem, with the graph im trying to get workin


Thankyou very much for your help, i will work on it later, and post the
result.

"The Scarlet Manuka" <sa...@maths.uwa.edu.au> wrote in message
news:8l157t$5oj$1@enyo.uwa.edu.au...

Quote
> D Yazbek <dyaz...@optushome.com.au> wrote:

> [...]

> > CONST gridx=10;
> >       gridy=10;

> > FUNCTION f(x:real):real;
> > BEGIN
> >   f:=(10*x*x*x)+3*sqr(x)+2*x+5;
> > END;

> > VAR gd,gm,mx,my,i:integer;
> >     x,y:real;

> > BEGIN
> [init graph and draw axes]
> >   mx:=getmaxX DIV 2;   {finds center x co-ord}
> >   my:=getmaxY DIV 2;   {finds center y co-ord}

> It would be best to have this *before* you draw the axes, and use these
> numbers for drawing the axes. That way if you decide you want to use a
> different mode you don't have to go and change all your hardcoded
> parameters.

> >   x:=(-my*gridx)/my;   {sets starting x point}

> This is clearly wrong, since its effect is to set x to -gridx, i.e. -10.
> Assuming you want to set x to the first value appearing on screen, it
> should be either "x := -mx / gridx" or "x := -mx*gridx" - you're using
gridx
> and gridy in inconsistent ways (more on that later).

> >   y:=(f(x)*my)/gridy;  {sets starting y point}

> You should use x and y in the same context. If you are using x as an
> argument to f, you are considering x to be the real (mathematical) value
of
> the point you want, so you should just have y as f(x). This formula also
> doesn't seem to make much sense. You are effectively multiplying y by
23.9.

> >   moveto(mx-my,my-trunc(y));

> I don't think you want mx-my here.

> >   setcolor(15);
> >   FOR i:=-my TO my DO {mx,my = (319,239) in 640*460}

> This should be "for i := -mx to mx do..."

> >     BEGIN
> >       x:=(i*gridx)/my;   {sets the next x co-ord to plot}
> >       y:=(f(x)*my)/gridy;{sets the next y co-ord to plot}
> >       lineto(mx+i,my-trunc(y));{joins the x coord, with the y coord with
a
> > line}
> >     END;

> Same problems here...

> You need to work out how to convert real coordinates to screen
coordinates,
> and vice versa. Also, what are you intending "gridx" and "gridy" for? As
> you're using it, the x axis has 10 pixels per unit, while the y axis has
0.1
> pixels per unit (i.e 10 units per pixel). This is confusing. Do you
actually
> want a domain of (-32, 32) and a range of (-2400, 2400)? [This may be
> reasonable for the problem...] If so, use gridx = 10 and gridy = 0.1 or
the
> reverse, and adjust your formulae appropriately.

> Your main loop should look something like this:

>   for i := -mx to mx do
>     begin
>       x := i / gridx;
>       y := f(x);
>       screenx := mx + i; {simpler than round(mx + x * gridx)}
>       screeny := round(my - y * gridy);
>       lineto(screenx, screeny);
>     end;

> You can get your first coordinate by duplicating this for i = -mx, i.e.
> (with some simplifications):

>   screenx := 0;
>   y := f(-mx / gridx);
>   screeny := round(my - y * gridy);
>   moveto(screenx, screeny);

> Here I've assumed that gridx and gridy represent the number of pixels per
> unit, so if you make them bigger you'll be able to fit less in. I imagine
> you'd want a pretty small domain for x and a somewhat larger one for y,
> perhaps something like (-5, 5) for x and (-100, 100) for y. You could thus
> try gridx ~ 60 and gridy ~ 2.5 - adjust these for your own desires. It's
> also a good idea to include some sanity checking (i.e. make sure your
points
> will be on the screen, although in most cases the graph unit will take
care
> of this for you; also, you might want to check that a reasonable
proportion
> of the graph will actually apear on screen, and if not print a warning or
> adjust the grid spacing to suit).

> Anyway, this should clear up your problems; see how you go.

> --
> ______________________________________________________________________
>      The Scarlet Manuka,      |        Nitpickers' Party motto:
>   Pratchett Quoter At Large,  |  "He who guards his lips guards his
>  First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
>    sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
> ______________________________|_______________________________________

Re:Additional problem, with the graph im trying to get workin


D Yazbek heeft geschreven in bericht ...

Quote
>Thankyou very much for your help, i will work on it later, and post
the
>result.

>"The Scarlet Manuka" <sa...@maths.uwa.edu.au> wrote in message
>news:8l157t$5oj$1@enyo.uwa.edu.au...
>> [...]
>> You need to work out how to convert real coordinates to screen
>coordinates,
>> and vice versa.
<snip>
>> Anyway, this should clear up your problems; see how you go.

>> --

Some more suggestions:

The screen is 640*480. (0,0) to (639,479)
Say you want to leave some room (80 pixels) for the legend,
that leaves the rectangle to draw in, at: (80,0) to (80,399)
Effectively a space of 560*400 pixels.
Note that the y-axis is 'upside down' so y here becomes
400-y to plot in.

Define constants and variables to keep track of
these shifts in pixels

Now define a couple of functions, to make the conversions
from real to plot coordinates (and maybe reverse).

function rx_sx(rx, factor_x:real,):integer;
{real x to screen x}
...
function sy_ry(sy:integer):real;
{plot y to real y}

If defined properly, you can experiment in text-mode if the
conversion actually fits:  rx_sx(sx_rx(x))=x.

Now calculate factor_x and factor_y to exactly match the
world-coordinates to the defined space (560*400) to draw in.
Then store them in (global) variables.
For this you have to know the range of the world-coordinates
(x_min,x_max) and (y_min,y_max) you want to plot.

Notice that the rectangle has more room in the x-direction than
in the y-direction. This gives a somewhat distorted view of the
math function. A rectangle of 400*400 would be better. That would
give the same 'grid-factor' for x and y.

Now make a procedure to actualy draw the line between to points.

procedure line2(rx1,ry1,rx2,ry2);
begin
    'calculate the conversion with the factors'
    'add the legend-shifts'
    'reverse the y-axis'
    lineto(..,..)
end;

You can also use this procedure to draw the x- and y-axis on a
world-coordinate basis.

Note: Keep everything as flexible as possible, so you can change
the space to draw in or the room for the legend or the position of
the rectangle, by simply changing one or two constants.

Hope this helps (some more)

Huub.

Other Threads