Plot a heart-shaped function in C#

example

This example is similar to Plot a smiley face function in C# except it shows how to plot a heart-shaped function defined by the following equations.

[y - Sqrt(2.5 × 2.5 - (x - 2.5) × (x - 2.5))] ×
[y - Sqrt(2.5 × 2.5 - (x + 2.5) × (x + 2.5))] ×
[(-y - Sqrt(2.5 × 2.5 - (x - 2.5) × (x - 2.5)))
    + Sqrt(x - 2.5) - Sqrt(x - 2.5)] ×
[(-y - Sqrt(2.5 × 2.5 - (-(x + 2.5)) ×
    (-(x + 2.5)))) + Sqrt(-(x + 2.5))
    - Sqrt(-(x + 2.5))] ×
[((y + 5) - Sqrt(2.5 × 2.5 - (x + 2.5) ×
    (x + 2.5))) + Sqrt(x + 2.5) - Sqrt(x + 2.5)] ×
[((y + 5) - Sqrt(2.5 × 2.5 - ((x - 2.5))
    × ((x - 2.5)))) + Sqrt(-(x - 2.5)) - Sqrt(-(x - 2.5))]
= 0

Unless I messed up transcribing these (which is possible), they define the shape shown in here.

The only real trick to this kind of equation is to note that A × B = 0 if A = 0 or B = 0 (or both). That means to graph this equation, you can graph each piece of it separately. That’s important for this example because the technique used depends on noticing when a value changes from less than zero to greater than zero. Because the pieces of this equation are imaginary for many values of x and y, they are often neither less than nor greater than 0, so that technique won’t work.

The program uses the following code to graph each of the function’s pieces.

PlotFunction(gr, F1, dx, dy);
PlotFunction(gr, F2, dx, dy);
PlotFunction(gr, F3, dx, dy);
PlotFunction(gr, F4, dx, dy);
PlotFunction(gr, F5, dx, dy);
PlotFunction(gr, F6, dx, dy);

The methods F1, F2, …, F6 return the pieces of the whole function.

For more information about how the program draws the plot, see these posts:


Download Example   Follow me on Twitter   RSS feed




This entry was posted in algorithms, graphics, mathematics and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to Plot a heart-shaped function in C#

  1. Pingback: Plot functions in C#, Part 3 - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *