Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Attempting to graph sinewave...

 
 
Old 03-19-2006   #1 (permalink)
Nick Howell


 
 

Attempting to graph sinewave...

I'm attempting to draw an arbitrary sinewave on-screen; I wanted to use
beziers, but I'm not smart enough, and I also want the ability to expand
to other mathematical functions, so doing a connect-the-dots
implementation seemed the best way to go.

I'm having some trouble with the drawing, though, and I'm not sure if
it's my algorithm, or I'm doing something stupid, or there's a bug in
the drawing code. I'm generating a geometry, and then putting it in a
path with a stroke. Here's the method that generates the geometry:

protected override ComplexGeometry GenerateGeometry(int Resolution)
{
if (Data == null)
{
return new ComplexGeometry();
}
StreamGeometry real = new StreamGeometry(), complex = new
StreamGeometry();

double xmin = 0, xmax = 100, xwidth = xmax - xmin, ymin = 50, ymax =
100, ywidth = ymax - ymin;
Converter<double, double> canvasXToGraph = delegate(double canvas)
{
return ((canvas - xmin) / xwidth) * VisibleRegion.Width +
VisibleRegion.Left;
},
graphXToCanvas = delegate(double graph)
{
return (graph - VisibleRegion.Left) / VisibleRegion.Width * xwidth
+ xmin;
},
canvasYToGraph = delegate(double canvas)
{
return ((canvas - ymin) / ywidth) * VisibleRegion.Height +
VisibleRegion.Bottom;
},
graphYToCanvas = delegate(double graph)
{
return (graph - VisibleRegion.Bottom) / VisibleRegion.Height *
ywidth + ymin;
};

StreamGeometryContext realContext = real.Open(), complexContext =
complex.Open();

realContext.BeginFigure(new Point(graphXToCanvas(VisibleRegion.Left),
graphYToCanvas(RealAt(VisibleRegion.Left, Time))), false, false);
complexContext.BeginFigure(new
Point(graphXToCanvas(VisibleRegion.Left),
graphYToCanvas(ImaginaryAt(VisibleRegion.Left, Time))), false, false);

for (int i = 0; i < Resolution; i++)
{
double d = VisibleRegion.Left + VisibleRegion.Width * (double)i /
(double)Resolution;

realContext.LineTo(new Point(graphXToCanvas(d),
graphYToCanvas(Data.CalculateReal(d, Time))), true, true);
complexContext.LineTo(new Point(graphXToCanvas(d),
graphYToCanvas(Data.CalculateImaginary(d, Time))), true, true);
}
realContext.Close();
complexContext.Close();

return new ComplexGeometry(real, complex);
}

And the type used by the Data property:

public class SinusoidalWaveData : WaveData
{
private double wavenumber;

public double Wavenumber
{
get { return wavenumber; }
set { wavenumber = value; FirePropertyChanged("Wavenumber"); }
}
private double angularFrequency;

public double AngularFrequency
{
get { return angularFrequency; }
set { angularFrequency = value;
FirePropertyChanged("AngularFrequency"); }
}
private double amplitude;

public double Amplitude
{
get { return amplitude; }
set { amplitude = value; }
}

public override double CalculateImaginary(double position, double time)
{
return Amplitude * Math.Cos(Wavenumber * position - AngularFrequency
* time);
}
public override double CalculateReal(double position, double time)
{
return Amplitude * Math.Sin(Wavenumber * position - AngularFrequency
* time);
}
public override double CalculateRealSlope(double position, double time)
{
return Wavenumber * CalculateImaginary(position, time);
}
}

I hope that held up well...can anybody see any pertinent errors, or is
there a much better way to do this? I wouldn't mind something with a
little better performance, although so far this hasn't been too bad (I
have a feeling it won't scale well).

Nick

My System SpecsSystem Spec
Old 03-19-2006   #2 (permalink)
Nick Howell


 
 

Re: Attempting to graph sinewave...

Well, ignore the request for bugs in the algorithm; I've corrected the
problems.

I'm still interested in any ideas as to a more efficient or cleaner way
to do this, though.

Nick
My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
defrag progress graph General Discussion
Help please! Graph render fail Vista music pictures video
Missing Graph Functinality Vista General
Resource Monitor - CPU graph Vista General
Resource Monitor - CPU graph Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46