I'm working on a project i need to find coordinates of pixels of selected area. I'm gaining this coordinates by simply clicking on a C# picture box. I need to find the pixel coordinates of the gray area as show in the picture in order to change the color of this ash area. is there a defined method in C# do do this? or please on how to archive this.
code samples will appreciated.
thanks in advance.
Selected Coordinates
Required Area
What you need is a point-in-polygon algorithm ( http://en.wikipedia.org/wiki/Point_in_polygon )
static bool PointInPolygon(Point p, Point[] poly)
{
Point p1, p2;
bool inside = false;
if (poly.Length < 3)
{
return inside;
}
Point oldPoint = new Point(poly[poly.Length - 1].X, poly[poly.Length - 1].Y);
for (int i = 0; i < poly.Length; i++)
{
Point newPoint = new Point(poly[i].X, poly[i].Y);
if (newPoint.X > oldPoint.X)
{
p1 = oldPoint;
p2 = newPoint;
}
else
{
p1 = newPoint;
p2 = oldPoint;
}
if ((newPoint.X < p.X) == (p.X <= oldPoint.X)
&& ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X) < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))
{
inside = !inside;
}
oldPoint = newPoint;
}
return inside;
}
(from http://www.gamedev.net/topic/533455-point-in-polygon-c-implementation/ )
You may also use the .Net HitTestCore Method if you use System.Windows.Shapes.Polygon to represent your polygon. I can't tell how easy that will work though.
Use the Click event, and pull out the mouse coordinates from the event. If the gray area is defined by a function, you can write a method to check if it's within the area specified. If not (it's just a static image), you should use the mouse coordinates to calculate which pixel you have clicked, and check its color value. There might be a method to get the color value where the mouse clicks (however, I might be confusing the method with the glReadPixel method in OpenGL).
Related
I want to to find a point is inside a trimmed surface or not. In the following pictures, think that i have a surface which has hole inside and also a point shown as red. How to check that the point inside a trimmed surface or not.
I used the following code but it doesn't work. In below code surfaceEntity is a Surface object in which red point inside. pntEntity is also an entity which is the red point in the below picture.
double x = (pntEntity.BoxMax.X + pntEntity.BoxMin.X) / 2;
double y = (pntEntity.BoxMax.Y + pntEntity.BoxMin.Y) / 2;
Point3D point3D = new Point3D(x, y, 0);
if (surfaceEntity.Trimming.IsPointInside(point3D))
{
Console.WriteLine("Inside");
}
else
{
Console.WriteLine("Not inside");
}
Image
You need first to compute 2D parametric coordinates of the point point3D using surfaceEntity.Project() or surfaceEntity.ClosestPointTo(). The resulting u, v values can then be used in the Trimming.IsPointInside() call.
Problem
How can i find the direction of one rectangle w.r.t to the other. The directions i am interested is up, down, left, and right. My rectangle is represented by a Cell class. I am trying to write a function in that cell class. Function accepts a parameter of Cell type the returns the direction either 1(up) 2(down) 3(left), or 4(right) of the passed cell w.r.t to the calling cell.
What i tried
I found the mid point of both the rectangles, and then compared the x, and y coordinates. But this technique is not working in all the cases. whenever i find a missing case, i have to include more and more if statements which i think is not a good programming practice. its becoming more and more error prone and difficult to understand.
While searching for the solution: maybe math.atan2() can work in my case. Maybe i can find the angle between mid points of these 2 rectangles and use the value of angle to determine the direction. But i am not sure if my thinking is correct.
Please guide me. Should i keep using my function and rectify it, or is there a better solution such as math.atan2()? A helping image for better understanding and a required solution is demonstrated below after the code.
Code
public int dirOfThisCell(Cell cell)
{
int dir = 0;
//find mid points of both cells
PointF midPointThis = this.computeAndGetMidPoint();
PointF midPointCell = cell.computeAndGetMidPoint();
//MessageBox.Show(mess);
//if x of both points is same or with little variance because of variance in sizes of cells
===>> //Comparison Starts!!
if (midPointThis.X > midPointCell.X)
{
//this cell is to the right.
if ((midPointCell.Y) == (midPointThis.Y))
{
dir = 3;
}
else if (Math.Abs(midPointCell.Y) - Math.Abs(midPointThis.Y) < 5) { dir = 3; }
else if (Math.Abs(midPointCell.Y) - Math.Abs(midPointThis.Y) > 5) {
if (midPointThis.Y > midPointCell.Y) { dir = 1; }
else if (midPointThis.Y < midPointCell.Y) dir = 2;
}
// a considerable difference
else { dir = 3; }
//some small variations in y
//else if(Math.Abs()
}
else if (midPointThis.X < midPointCell.X)
{
// this cell is to the left
if ((midPointCell.Y) == (midPointThis.Y))
{
dir = 4;
}
else if (Math.Abs(midPointCell.Y) - Math.Abs(midPointThis.Y) <= 10)
{
dir = 4;
}
}
//if this cell is below
else if (midPointThis.Y > midPointCell.Y)
{
//this cell is down than the cell
if ((midPointCell.X) == (midPointThis.X))
{
dir = 1;
}
//else if (Math.Abs(midPointCell.X) - Math.Abs(midPointThis.X) < 2) { dir = 1; }
}
else if (midPointThis.Y < midPointCell.Y)
{
if ((midPointCell.X) == (midPointThis.X))
{
dir = 2;
}
}
return dir;
}
Image
Sample Image
Sample rectangles are shown in the picture number wise. The rectangle can be of single cell or made by combining multiple numbered cells.
Sample Solution required
Direction of cell 18 w.r.t to 8 should be up(1)
Direction of cell 18 w.r.t to 10 should be up(1)
Direction of cell 14 w.r.t to 13 should be right(4)
Direction of cell 9 w.r.t to 31 should be down(1)
Direction of cell 15 w.r.t to 9 should be left(3)
I am working in c#.
Any help would be much appreciated.
Thank You
Calculating the angle of the center of a rectangle compared to the center of another does not seem to be a very good idea, because the center of those rectangles are only telling you where their center is and the solution is completely reluctant to the width, height and direction of the sides. I know that the third one is not a concern in your specific case as you can safely assume that the sides are horizontal XOR vertical, but in general terms, that could be an issue as well. To calculate the relative position of Shape1 (which is a rectangle in our particular case) compared to Shape2, you need to calculate the minimum and maximum x and y for both.
Shape1 is to the left of Shape2 <=> Shape1.maxX <= Shape2.minX
Shape1 is to the right of Shape2 <=> Shape2.minX >= Shape2.maxX
Shape1 is above Shape2 <=> Shape1.maxY <= Shape2.minY
Shape2 is below Shape2 <=> Shape2.minY >= Shape1.maxY
I'm attempting to make a very very simple paint project in C#'s WPF. I have pretty much everything working except for drawing a triangle. I can't figure out the math required to create the triangle.
I have code like this for drawing rectangles and circles:
case "Rectangle":
case "Ellipse":
var x = Math.Min(pos.X, mm.startPoint.X);
var y = Math.Min(pos.Y, mm.startPoint.Y);
var width = Math.Max(pos.X, mm.startPoint.X) - x;
var height = Math.Max(pos.Y, mm.startPoint.Y) - y;
mm.shapeObj.Width = width;
mm.shapeObj.Height = height;
Canvas.SetLeft(mm.shapeObj, x);
Canvas.SetTop(mm.shapeObj, y);
break;
I add it to the children of the canvas elsewhere. This code allows me to click on the canvas and drag my mouse to size the rectangle or ellipse.
I was hoping to do something similar with a triangle. I only want the user to be able to click on the screen and drag out an equilateral triangle, for simplicity sake. I thought about making it so the user could just click three times to create the triangle but due to the way the project is coded, that would be a little more difficult than it sounds. However, if there isn't any way to calculate the triangle I'm trying to create, I can make it work with three clicks.
if you order your points like this:
int smX = startPoint.X < finalPoint.X ? startPoint.X : finalPoint.X;
int bgX = startPoint.X < finalPoint.X ? finalPoint.X : startPoint.X;
int smY = startPoint.Y < finalPoint.Y ? startPoint.Y : finalPoint.Y;
int bgY = startPoint.Y < finalPoint.Y ? finalPoint.Y : startPoint.Y;
You can imagine a rectangle with the points:
(smX, smY)
(smX, bgY)
(bgX, smY)
(bgX, bgY)
So you could use the middle of the rectangle to set a point of triangle, than draw a triangle with the points:
(smX, bgY)
(bgX, bgY)
(smX + ((bgX - smX) / 2), smY)
Actually my robot wants to move from source to target with obstacle avoidance. I find out the obstacle(rectangle shape) and Target(circle shape) in pixels. But i don't know how to find the path from source to target... Please help me.
Here is the code for finding obstacle and target.
for (int i = 0, n = blobs.Length; i < n; i++)
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
AForge.Point center;
float radius;
// is circle ?
if (shapeChecker.IsCircle(edgePoints, out center, out radius))
{
g.DrawEllipse(whitePen, (float)(center.X - radius), (float)(center.Y - radius),
(float)(radius * 2), (float)(radius * 2));
target.Add(center.ToString());
}
else
{
List<IntPoint> corners;
// is triangle or quadrilateral
if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
{
// get sub-type
PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
Pen pen;
if (subType == PolygonSubType.Unknown)
{
pen = (corners.Count == 4) ? redPen : bluePen;
}
else
{
pen = (corners.Count == 4) ? greenPen : brownPen;
}
g.DrawPolygon(pen, ToPointsArray(corners));
}
}
}
This above coding will detect obstacle and target position pixel values and store it in a seperate array. But from these pixel values how to calculate the path? Waiting for ur suggestions.....
Trying looking up the A* search algorithm.
I have not looked into your code but it is a classic path finding problem. One suggestion could be to map the entire area the robot moves onto a grid. The grid can have discrete cells. And then you can use any graph search algorithm to find a path from start cell to goal cell.
You can use few of the algorithms, like Dijkistra, Best-first and A-Star search algorithms. It turns out that A-Star is efficient and easy to implement. Check this, contains a nice explanation about A-Star.
I want to move through the pixels of an image, not by going line by line, column by column in the "normal" way. But begin at the center pixel and going outward in a spiral motion. But I'm not sure how to do this.
Any suggestions on how this can be done?
You can do this by using parametric functions, function for radius is r(t) = R, and x(t) = Rcos(t) and y(t)=Rsin(t).
Do you mean something like this?
It would be helpful to think about this in reverse.
For example, starting at the top left corner and moving in a clockwise direction you would move along the top row, then down the right hand side, along the bottom, and up the left edge to the pixel under the starting point.
Then move along the second row, and continue in a spiral.
Depending on the dimensions of the image you will end up with either a single column of pixels or a single row of pixels and will be moving either up/down or left/right.
From this finishing point you can then follow your steps backwards and process all the pixels as you need to.
To work out your starting position mathematically you would need to know the width/height of the image as well as which pixel you would like to end on and the direction you want to be travelling in when you get to the last pixel.
Something like this should do it:
int x = width / 2;
int y = height / 2;
int left = width * height;
int dir = 0;
int cnt = 1;
int len = 2;
int[] move = { 1, 0, -1, 0, 1 };
while (left > 0) {
if (x >= 0 && x < width && y >= 0 && y < height) {
// here you do something with the pixel at x,y
left--;
}
x += move[dir % 4];
y += move[(dir % 4) + 1];
if (--cnt == 0) {
cnt = len++ / 2;
dir++;
}
}
If the image is not square, the spiral will continue outside the coordinates of the image until the entire image has been covered. The condition in the if statement makes sure that only coordinates that are part of the image are processed.