2

I am very new to c# System Draw so please do help me on my code. i am trying to plot the quadratic equation curve and is using the "for" loop in order to dot 10 coordinates for the curve. i have tested out this code many times and nothing ever appears when i start the code. Also whenever i run the code, i get the message ArgumentException was Unhandled, Parameter is not valid with the code " g.DrawCurve(aPen, Points);" highlighted. please help me on this problem i have spent many days trying to fix.

{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {

        float a = 10, b = 30, c = 10;
        double x1, x2, delta, cx1, cx2, y1, y2;
        int icx1, iy1, icx2, iy2;
        delta = (b * b) - (4 * a * c);
        x1 = ((b * (-1)) + Math.Sqrt(delta)) / (2 * a);
        x2 = ((b * (-1)) - Math.Sqrt(delta)) / (2 * a);
        for (int i = (-10); i <= 10; i = i + 1)
        {
            cx1 = i * x1;
            cx2 = i * x2;
            y1 = (cx1 * cx1 * a) + (cx1 * b) + c;
            y2 = (cx2 * cx2 * a) + (cx2 * b) + c;
            icx1 = Convert.ToInt32(cx1);
            iy1 = Convert.ToInt32(y1);
            icx2 = Convert.ToInt32(cx2);
            iy2 = Convert.ToInt32(y2);


            Graphics g = e.Graphics;
            Pen aPen = new Pen(Color.Blue, 1);
            Point point1 = new Point(icx1, iy1);
            Point point2 = new Point(icx2, iy2);
            Point[] Points = { point1,point2 };
            g.DrawCurve(aPen, Points);
            aPen.Dispose();
            g.Dispose();


        }
RabbitBadger
  • 539
  • 7
  • 19
  • Start with drawing a line with **two** points with fixed/constant/not calculated coordinates. – DrKoch Feb 09 '15 at 08:02

2 Answers2

4

The key problem is that the code disposes the Graphics object. On the second iteration the Graphics object has been disposed and the call to DrawCurve will fail.

And as mentioned in the comments, the DrawCurve method expects 3 points in the array. See under Remarks on the MSDN Page for DrawCurve

All other Dispose calls for the Pen should be reduced as much as possible to prevent re-creating so many pens.

As for the graph: I am not entirely sure what you are trying to do but if you are trying to draw a parabola, you should not solve the quadric equation but instead put the x value in the equation.

Pseudo code:

for x = -10 to 10 step 3

    if SavePoint == null

        x1 = x
        y1 = a * x1 * x1 + b * x1 + c

        point1 = TransformToLocalCoordinates(x1, y1)

    Else

        point1 = SavePoint

    End if

    x2 = x + 1
    y2 = a * x2 * x2 + b * x2 + c

    point2 = TransformToLocalCoordinates(x2, y2)

    x3 = x + 2
    y3 = a * x3 * x3 + b * x3 + c

    point3 = TransformToLocalCoordinates(x3, y3)

    DrawCurve point1, point2, point3

    SavePoint = point3

next
Emond
  • 50,210
  • 11
  • 84
  • 115
  • 1
    `DrawCurve` also needs at least three points, so it fails even before the first `Dispose`. – Luaan Feb 09 '15 at 08:06
1

Don't dispose of the Graphics and Pen instances - you're doing that every step of your cycle.

Instead, get one instance of the Pen (and note that you can use the global Pens.Blue :) ), and don't dispose of it, or the Graphics object.

Also, try using DrawLine instead of DrawCurve for a start - it will not give you nicely anti-aliased graph, but it's a lot easier. Only start with DrawCurve once you understand how to use it properly :) One of the points being that you can't draw it through just two points, of course - you need at least three.

DrawCurve draws a spline through all the specified points. So in fact, you can only call it once, with all the points of the quadratic you've pre-calculated. This will give you a nicely rendered curve. However, I'm not sure if it will actually be a real quadratic - I'm not sure if GDI+'s splines are quadratic or (more likely) cubic. In any case, it will not work for exact renderings of different curves.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Do you know that this is the answer to the OP's question? – Enigmativity Feb 09 '15 at 08:03
  • @Enigmativity Well, not the complete answer, sure - but fixing the equations isn't really the job for SO, is it? :D – Luaan Feb 09 '15 at 08:05
  • i feel very dumb witted for not knowing this, thank you very much for your help. Although now my curve line just looks like a bunch of straight lines over lapsing one another at the edge of my screen. Do you have any thoughts on this as well? – RabbitBadger Feb 09 '15 at 08:05
  • This isn't an answer. It's a long comment. Erno has found the true problem. – Enigmativity Feb 09 '15 at 08:07
  • @MichaelEk I'm not quite sure how your code is supposed to work, it really looks unnecessarily complicated. You might want to start from a different form of the quadratic equation :) – Luaan Feb 09 '15 at 08:13
  • @MichaelEk Oh, I see, you're solving the equation for zero, and then using the intersections to plot the rest, that's not going to work. Try putting the numbers you've calculated into the equation by hand and you'll see they don't actually satisfy it (except for the value of `1` and `-1`, of course). You're not supposed to be solving for `y = 0`, instead, just calculate the proper `y` for each `x`, e.g. `y = i * i *a + i * b + c`. You always need two points, of course, but that just means you need to remember the last point, for example. – Luaan Feb 09 '15 at 08:21