Uniform Grid Subdivision of Points in C# - c#

I have a set P of 2D points that I could like to cluster in a 2D uniformly spaced grid, where each cell is length X.
I want to do this because I am trying to create a heat map, and I have way to much information so I am hoping by clustering the points into a uniformly spaced grid I can just report the final count of each grid.
Thanks!
if It makes any difference I am getting my information via SQL (the points) that are within a certain radius of a specified point first prior to subdivision.

Are you looking for something like this?
var result = from p in points
group p by new { X = p.X / length, Y = p.Y / length } into g
select new
{
g.Key.X,
g.Key.Y,
Count = g.Count()
};
I don't know if there's a way to take advantage of the order of points.

Related

Map projection with System.Drawing.Graphics

I have a set of geo coordinates as latitude/longitude pairs that I wish to project onto a 2D surface. Some of the coordinates are connected by lines forming a shape/outline/polygon.
I understand how to project individual points using one of the many available map projections like Mercator and then drawing them with Graphics.DrawArc but how do I go about projecting the connecting lines between them? I can't just project the two defining coordinates of a line and draw it Graphics.DrawLine because every single point on that line has to be projected as well, right? I don't know much about these things, so I hope you understand what I mean.
Is it even possible to do what I'm trying to do using just the methods provided by the System.Drawing.Graphics class? Can I do this with a projection matrix? If anyone could explain a bit how I would go about doing this, I would greatly appreciate it. Thanks.
If your are only drawing lines it is probably easiest just to subdivide the lines into short segments, project each segment point and draw straight lines between them. This might not be the most performant or exact way to do it. But it should be fairly easy to implement.
For example:
public static void DrawSubdivided(this Graphics g, Pen pen, Vector2 p1, Vector2 p2, float subdivisionLength)
{
var d = p2 - p1;
var length = d.Length();
// Add check for zero-length lines
var dn = d / length;
var points = new List<Vector2>();
for (float l = 0; l < length; l += subdivisionLength)
{
points.Add( p1 + dn * l);
}
points.Add(p2);
// apply transformation to the points
g.DrawLines(pen, points.Select(p => new PointF(p.X, p.Y)).ToArray());
}
This uses System.Numerics.Vector2 since it has reasonable arithmetic operations defined, in contrast to PointF.

Call function when chart y-value exceeds line's y-value

I have a candlestick chart which automatically updates with real time prices from a cryptocurrency exchange in the .NET forms. The goal is to make the bot preform actions when the price on chart passes one of the lines drawn by the user. So far I've come to the point of enabling line-drawing for users thanks to this article.
Could anyone please point me towards a method of detecting collision between the chart candles and the drawn lines? I feel like there must be an easier way than what I'm thinking of currently, just can't seem to figure out the way to it.
Using the exact solution for the line drawing as in the article, also posted code for the line-drawing below:
int index1 = 1;
int index2 = 4;
DataPoint left = chart.Series[0].Points[index1];
DataPoint right = chart.Series[0].Points[index2];
//Init the annotation
LineAnnotation line = new LineAnnotation();
line.AxisX = chart.ChartAreas[0].AxisX;
line.AxisY = chart.ChartAreas[0].AxisY;
line.IsSizeAlwaysRelative = false;
//Each point in a candlestick series has several y values, 0=high, 1=low, 2=open, 3=close
line.Y = left.YValues[1]; //low
line.X = left.XValue;
//If your data is indexed (your x values are Strings or you've set Series.IsXValueIndexed to true), use the data point index(+1) as the line X coordinate.
//line.X = index1 + 1;
//Use the width and height properties to determine the end position of the annotation.
line.Height = right.YValues[1] - left.YValues[1];
line.Width = right.XValue - left.XValue;
//Again, use the index if necessary
//line.Width = index2 - index1;
chart.Annotations.Add(line);
Just looking for a point in the direction of an easier solution, not the solution itself :) Thanks in advance!
So it sounds like you are asking is if a Point (Geometry) is above or below a line.
Here are the assumption (which you can change later to fit your needs):
an external resource is giving you a specific value (Y) at a specific point in time (X), which will call the Integral point XY.
The user has drawn a line which gives you a starting point (x1, y1) and an end point (x2, y2).
The graphs X component is in minutes, with each tick horizontally is 1 minute.
The graphs Y component is in dollars, with each tick is $25.
The user has drawn a line from (1:00pm, $50) to (1:05pm, $75).
We get an Integral Point XY at 1:10pm of $125.
What is the value of the line at 1:10pm so you can compare it to the Integral Point XY.
Based on my comments of Trigonometry..
We know the adjacent length is: 1:05 - 1:00 = 5
We know the opposite length is: 75 - 25 = 50
Using the formula: atan(opposite / adjacent) = angle
We calculate that the angle is: atan(50 / 5) = 1.47112767rad (radians)
Now we simply reverse our math:
We know the adjacent length is: 1:10 - 1:00 = 10
We know our Angle in Radians: 1.47112767
Using the formula: adjacent * tan(angle) = opposite
We calculate that the opposite is: 10 * tan(1.47112767) = ~$99.999999 or $100
$125 is above $100, do what you want.

How can I store the location of the pixels with colour value using C#?

I am making some lists that holds the colour value (0 for black and 1 for white) for each pixel across my image. My problem is when I finish each list it just gives me a single dimensional array that only has got 0s and 1s but I don’t know to which pixel it belong when I want to drew the output image.
Can anyone tell me if I can store location of the pixels as well as the colour value both at the same time in my list? Or any other alternative?
Answering following questions:
Can anyone tell me if I can store location of the pixels as well as
the colour value both at the same time in my list? Or any other
alternative?
If using the .NET Framework 4 or higher, you could use a Tuple to store the values. Fill tuple following way:
var LocXLocYColor = new Tuple<int, int, bool>(1, 1, true);
You could loop through all your these values, using a foreach:
int locx, locy;
bool color;
foreach(var itm in LocXlocYColor)
{
locx = itm.Item1;
locy = itm.Item2;
color = itm.Item3;
}
More Tuple information: MSDN
Above code can be used, when you want to store the pixel locations seperate as integers.
Making use of a Dictionary is another way to achieve your needs:
Create a new dictionary:
Dictionary<Point, bool> locationColor = new Dictionary<Point, bool>();
Fill dictionary with location and color:
locationColor.Add(new Point(1, 1), true);
...
Loop over items in dictionary:
Point location;
bool color;
foreach(KeyValuePair<Point, bool> itm in locationColor)
{
location = itm.Key;
color = itm.Value;th entry.Value or entry.Key
}
If using Point, don't forget to include: System.Drawing; on top of your class.
More Point information: MSDN
Dictionary<Drawing.Point, int> pixelLocations = new Diciontary<Drawing.Point, int>();
Will do what you want.
Edit: Unless you're not storing your locations as points. If they're not Points, then just substitute Drawing.Point for whatever datatype you're using.
You can use your list to know the location of the pixels (this is assuming you know the width or height).
If the pixels were arranged so that the top row of pixels was stored, then the second, etc, you could get the locations like this (where the origin is 1,1):
mylist[wantedY * width - (width - wantedX - 1)]
Where wantedY is the Y-location and wantedX is the X-location.
If, however, the pixels are arranged by column (so that the pixels with x = 0 were taken, then x = 1, etc), you can just use:
mylist[wantedX * height - (height- wantedY - 1)]
Instead of using a list, use a 2-dimensional array
bool[,] isWhite = new bool[bmp.Width, bmp.Height];
and store the values at their corresponding place in this array
isWhite[x, y] = theColor == Color.White;
The location of the pixel is the location within the array. There is no need to store it separately.

Largest Empty Rectangle Within a Rectangle

I'm not really good in maths, so I'm having really hard times converting formulas into code, and I can't find anything ready-made googling around. I have a big rectangle containing a lot of small rectangles... and all what I need to do is to calculate the largest empty rectangle. Anyonne can help me?
Here is what I came up with... nothing to say, it's a big fail.
Rect result = new Rect();
for (Double l = 0; l < bigRect.Width; ++l)
{
for (Double t = 0; t < bigRect.Height; ++t)
{
Double h = 0;
Double w = 0;
while ((h <= bigRect.Width) && (w <= bigRect.Height))
{
Rect largestEmpty = new Rect(l, t, w, h);
if (smallRects.TrueForAll(smallRect => !smallRect.IntersectsWith(largestEmpty)) && ((largestEmpty.Height * largestEmpty.Width) > (result.Height * result.Width)))
result = largestEmpty;
else
break;
++h;
++w;
}
}
}
From your Perdue Docs Link it says there is a set (let's call it ASD) of points in the Big Rect and you would to have find the largest Rect containing no points of the set ASD. Looking at your code, it seems you didn't (directly) incorporate these points. I would extract the points from the smaller Rects ans create set ASD. Since your working in type double, you should have access to the points, otherwise the algorithm would have a significantly higher run time since you need to check all possible doubles in a specific range (the entire Big Rect). Using the points, I would trying find the points with the shortest distance form each other (sqrt(dx^2+ dy^2)) (shortest shouldn't contain any points) then go to the next shortest and see if any points are contained and etc. In other words, create a order list of all combinations (not permutations, (a,b) to (c, d) should be == (c, d) to (a,b)) ordered by the distance in between them. Might not be optimal, but gets the job done.
EDIT: All order pair besides the diagonals of the smaller Rects should be in the order list, since the smaller Rects should not be conatined, You can also exclude pairs with the same x or y value.

c# equation is drawing a hole in the graph

for (double x=0;x<=7D;x+=.01D)
{
b = 1.771289; c = 2.335719; d = 0.5855771; g = 4.4990302; h = 4.3369349; k = 0.67356705;
y = b * Math.Exp(-(0.5 * (Math.Pow(((x - c) / d), 2)))) +
g * Math.Exp(-(0.5 * (Math.Pow(((x - h) / k), 2))));
qResults.Rows.Add(x, y);
}
the graph is good but it draws a hole in the peek.i am using mschart:
http://imageshack.us/photo/my-images/824/graph1v.png/
i would like to know whether the hole is a problem with my syntax?
It seems that your y-axis range is bounded by the maximum value, but the very point falls exactly outside the plotting range.
One solution is to add a small amount to the axis range such that all points fall clearly inside the plotting space.
Try making the max y range for the graph a little over the max value. If the max value is 4.5 then make the graph y-axis limit equal to 5.0.
There's nothing wrong with your syntax, that really should be a smooth curve. I stuck it into matlab just to be certain.

Categories

Resources