Coder Social home page Coder Social logo

cartesian-core's Introduction

cartesian-core

.net library that translates any x,y data to pixel coordinates

  • CartesianCore the library itself.
  • CartesianCanvasDrawer an example project that draws graph on a canvas.
  • CartesianBitmapDrawer an example project that draws graph on a bitmap.

public methods

CartesianCore.Plane

class constructor

public Plane(int width, int height, int topMargin = 0, int bottomMargin = 0, int leftMargin = 0, int rightMargin = 0)

initializes a new plane

width: width of the area plane to be drawn (pixel)
height: height of the area plane to be drawn (pixel)
topMargin: margin from top to the plane (pixel)
bottomMargin: margin from bottom to the plane (pixel)
leftMargin: margin from left to plane (pixel)
rightMargin: margin from right to plane (pixel)

CartesianCore.Plane.AddDataset

adds dataset to a plane to be processed and sets plane's maximum & minimum values of each x,y axis.

public void AddDataset(Dataset dataset)

dataset: an instance of a Dataset class

CartesianCore.Plane.SetAxes

Sets plane's maximum & minimum values of each x,y axis and makes plane ignores calculated maximum & minimum values of each axis from added datasets
This can be called before public void AddDataset(Dataset dataset)

public void SetAxes(double xMin, double xMax, double yMin, double yMax)

xMin: minimum value of X axis
xMax: maximum value of X axis
yMin: minimum value of Y axis
yMax: maximum value of Y axis

CartesianCore.Plane.GetDataset

Gets translated coordinates of the given dataset.

public Point[] GetDataset(int datasetID)

Point: A structure that holds X and Y double values

datasetID: id of the dataset given when created

CartesianCore.Plane.GetHorizantalLine

Gets a line perpendicular to given y point along the plane width.

public Point[] GetHorizontalLine(double y)

Point: A structure that holds X and Y double pixel coordinates

y: value of the y point on the Y axis

CartesianCore.Plane.GetVerticalLine

Gets a line perpendicular to given x point along the plane height.

public Point[] GetVerticalLine(double x)

Point: A structure that holds X and Y double pixel coordinates

x: value of the x point on the X axis

CartesianCore.Plane.GetPoint

Gets a point

public Point GetPoint(double x, double y)

Point: A structure that holds X and Y double pixel coordinates

x: value of the x point on the X axis
y: value of the y point on the Y axis

CartesianCore.Dataset

class constructor

public Dataset(int datasetID)

initializes a new dataset
represents a set of data in (x,y) pairs

datasetID: id of the dataset to be passed as an argument to Plane.GetDataset(int datasetID) function

CartesianCore.Dataset.Add

Adds data (x,y) pair to the dataset

public void Add(double xValue, double yValue)

xValue: value of the x point on the X axis
yValue: value of the y point on the Y axis

Example Usage

CartesianCanvasDrawer

In order to make use of CartesianCore library you need to add compiled CartesianCore.dll file to your project as a reference. First lets generate some data to draw for the sake of example.

var r = new Random();
int resolution = 1;
int sample = 1200 / resolution;
int timeStart = -600 / resolution;
float coef = 0.005f * resolution;
timeData = new float[sample];
sinData = new float[sample];
someData = new float[sample];

for (int i = 0; i < sample; i++)
{
	timeData[i] = (i + timeStart) * coef;
}

for (int i = 0; i < sample; i++)
{
	var t = (i + timeStart) * coef;
    sinData[i] = (float)Math.Cos(4*t);
    someData[i] = t * (float)Math.Sin(Math.Pow(t, 2));
}

Now we have some data to show in cartesian plane.
lets create a plane first

var plane = new Plane(width, height, 50, 50, 50, 50);

we need two dataset instances since we have 2 different data with id of 1 and 2 respectively.

Dataset dataset_A = new Dataset(1);
Dataset dataset_B = new Dataset(2);

put the generated data to datasets
timeData array is representing the X axis for both dataset

for (int i = 0; i < sample; i++)
{
	dataset_A.Add(timeData[i], someData[i]);
	dataset_B.Add(timeData[i], sinData[i]);
}

now we need to put these datasets to the plane instance.

plane.AddDataset(dataset_A);
plane.AddDataset(dataset_B);

maximum and minimum values are calculated for each axis. its better to floor the minimum values and ceil the maximum values of each axis to get better view on the cartesian plane.
you can comment out the code below to see the differences.

plane.SetAxes(MathTools.Floor(plane.XAxisMin), MathTools.Ceil(plane.XAxisMax), MathTools.Floor(plane.YAxisMin), MathTools.Ceil(plane.YAxisMax));

MathTools static class has an advance Floor and Ceil functions that floors and ceils given value to any number passed as an argument.
or if you know your data you can set the boundries of the plane directly like below.

plane.SetAxes(-4, 4, -5, 5);

For drawing strings to canvas we need to define a class derived from FrameworkElement
we will use this class to draw numbers to cartesian plane
Now lets start to draw plane since we set our maximum and minimum values to an integer above we can draw the vertical grids
this is optional you don't need to draw them

for (int i = (int)plane.XAxisMin; i <= plane.XAxisMax; i++)
{
	points = plane.GetVerticalLine(i);
	DrawLines(CanvasObj, points, color, 1);
	DrawString(CanvasObj, $"{i}", plane.GetPoint(i, 0), 14, Colors.LightGray, new Typeface("consolas"));
}

DrawLines and DrawString functions are defined only for WPF canvas element.
They have nothing to do with CartesianCore library.

We can do the same for the horizontal grids

for (int i = (int)plane.YAxisMin; i <= plane.YAxisMax; i++)
{
	points = plane.GetHorizontalLine(i);
	DrawLines(CanvasObj, points, color, 1);
	DrawString(CanvasObj, $"{i}", plane.GetPoint(0, i), 14, Colors.LightGray, new Typeface("consolas"));
}

Draw the axes. First X axis

points = plane.GetHorizontalLine(0);
DrawLines(CanvasObj, points, Colors.White, 1);

I prefer lighter color to highlight the axes since I choose the dark backround.

Then Y axis

points = plane.GetVerticalLine(0);
DrawLines(CanvasObj, points, Colors.White, 1);

Now we can draw our data. Remember we need to use the ids given previously to get them.
Ids are useful for customizing any dataset.

I know sine wave dataset id is 2 so I want them to display green

points = plane.GetDataset(1);
DrawPoints(CanvasObj, points, Colors.Blue, 3);

points = plane.GetDataset(2);
DrawPoints(CanvasObj, points, Colors.Green, 3);

cartesian-core's People

Contributors

bariseksi avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.