KarthikRIyer/swiftplot

Add support to plot functions in LineGraph

KarthikRIyer opened this issue · 3 comments

Add support to plot functions in LineGraph

As a comment on the design of this, it'd be great to have this use a function / closure as a parameter for this. Something of the type (Numeric) -> (Numeric) for an f(x), for example. This would allow for arbitrarily complex functions to be passed in. Might have to think about the signature for that, to account for parametric functions with more than one Y value at a given X, though.

You'd have to account for what happens in NaN cases, and may need to think about what numeric types to support for this.

You'd also maybe think about whether to have these in parallel to the Series array that's currently present in the LinePlot (as a separate array of Function2D or whatever), or if you'd want to create an encompassing type or protocol that could include both Series and functions. Something that would provide values in response to queries for a specific X coordinate, maybe. That's all something that could be architected when you come to it, however.

I have overloaded the addSeries function with (Float)->Float as a parameter at the moment. I'll get simple functions to plot first and then move on to multiple Y and NaN cases.
I am also accepting the number of samples also as an Int.
Does that seem fine?

But first I'll have to change the plotting logic. I don't know how or why, but I forgot to consider negative values😅 So now the line goes below the x axis and out of the image when you give negative values.

That sounds like a good plan for now. Best not to over-architect things initially, and build or revise interfaces as they are needed for different capabilities.

Yeah, the lack of negative support is a great example of why the little test applications are so useful to have early on. I always forget something, and building against the test applications and checking their output helps to point out errors. It's not really test driven development, but it's in the same ballpark.