I have a WPF polygon which can have variable number of sides. I would like to get the X, Y Coordinates of each intersection dynamically.
I am able to get the Relative Point of the polygon using the below code
Point relativePoint = polygon.TransformToAncestor(LayoutRoot)
.Transform(new Point(0, 0));
If the polygon is square I am able sort of get the coodinates by adding the Height and width to the relative point.
double polygonWidth = polygon.ActualWidth/2;
double polygonHeight = polygon.ActualHeight/2;
But if it is not square how can get all the X,Y cordinates for the intersection.
Given a point in coordinates relative to the Polygon, you may easily check this:
if (polygon.RenderedGeometry.FillContains(point))
{
...
}
If you are going to check that on a mouse click, you can get the relative point by calling
var point = e.GetPosition(polygon);
where e is a MouseButtonEventArgs.
Related
I'm building a paint-esque program where the user can draw shapes like rectangles and ellipses on the canvas. These shapes have two coordinates stored, the corner from when the user started to draw it and the corner where the user stopped drawing it. I want to be able to rotate these shapes 90 degrees clockwise around the center of the bitmap they are drawn on, this bitmap is 600 x 600 pixels large. How can I transform the avaliable coordinates of these shapes so that they appear rotated 90 degrees.
This is the code that I have been trying to make work:
foreach (TekeningElement t in getekende_elementen)
{
int x_begin = t.GetBeginPunt.X; //store x coordinate of starting point
int y_begin = t.GetBeginPunt.Y; //store y coordinate of starting point
int x_eind = t.GetEindPunt.X; //store x coordinate of end point
int y_eind = t.GetEindPunt.Y; //store y coordinate of end point
int x_verschil = x_eind - x_begin; //calculate width
int y_verschil = y_eind - y_begin; //calculate height
t.SetBeginPunt = new Point(600 - x_begin - y_verschil, x_begin); //set new starting point
t.SetEindPunt = new Point(600 - x_eind + y_verschil, x_eind); //set new end point
}
Thanks for the help.
My issue is that I've been trying to check if a rectangle that is rotated by a certain amount of degrees contain a point, but I wasn't able to calculate that after many attempts with the help of some code samples and examples that I've found online.
What I got is the rectangle (X, Y, Width, Height, Rotation) and the point (X, Y) and I've been trying to create a simple function that lets me instantly calculate that, which would be something something like this:
public bool Contains(Rect Rectangle, float RectangleRotation, Point PointToCheck);
But as I mentioned, I wasn't able to do so, those mathematical calculations that include some formulas I found online are way too much for me to understand.
Could someone help me with calculating this? If you could provide the calculation in C# code form (not formulas) then that would be great! Thanks.
PS: Using the 2D Physics Engine that is available in Unity3D is not a option, my rectangle is not associated with a gameobject that I could attach a 2D collision component to, I need to do this mathematically without the involvement of gameobjects or components.
Edit: I forgot to mention, the rectangle is being rotated by the middle of the rectangle (center/origin).
Rather than checking if the point is in a rotated rectangle, just apply the opposite of the rotation to the point and check if the point is in a normal rectangle. In other words, change your perspective by rotating everything by -RectangleRotation, so that the rectangle does not appear rotated at all.
public bool Contains(Rect rect, float rectAngle, Point point)
{
// rotate around rectangle center by -rectAngle
var s = Math.Sin(-rectAngle);
var c = Math.Cos(-rectAngle);
// set origin to rect center
var newPoint = point - rect.center;
// rotate
newPoint = new Point(newPoint.x * c - newPoint.y * s, newPoint.x * s + newPoint.y * c);
// put origin back
newPoint = newPoint + rect.center;
// check if our transformed point is in the rectangle, which is no longer
// rotated relative to the point
return newPoint.x >= rect.xMin && newPoint.x <= rect.xMax && newPoint.y >= rect.yMin && newPoint.y <= rect.yMax;
}
I do not know how to approach this problem as I am using API that has Plane data structure that is basically origin point, x y and z vectors that define a plane.
If I have two planes, how can I find bisector plane?
Is there a mathematical description for such plane.
Geometrically I would approach this problem by calculating intersection line between planes and then no idea how to define a point for direction that plane.
Any help would be much appreciated.
Before I tried something like this, and this get what I want, but I am wondering if there is a solution without doing intersections:
public static Plane BisectorPlane(Plane a, Plane b)
{
Rhino.Geometry.Intersect.Intersection.PlanePlane(a, b, out Line lnA);
a.Translate(a.ZAxis);
b.Translate(b.ZAxis);
Rhino.Geometry.Intersect.Intersection.PlanePlane(a, b, out Line lnB);
return new Plane( lnA.From,lnA.To,lnB.PointAt(0.5));
}
I am wondering if it is possible to solve this is not geometrically (calculating intersections) but mathematically.
You already have got line of intersection. Now choose any point P at this line to make base point.
Get direction vector for this line (dL)
Get sum of unit normals for given planes S. This is vector lying in bisector plane and perpendicular to intersection line
S = (a.normal + b.normal)
Now calculate vector product of dL and S to get normal
BisectorN = dL x S
And normalize it (make unit length dividing by its length) if needed
bN = BisectorN.Normalize
Base point P and normal bN define bisector plane.
I tried your approach and it gives bisector plane. But the problem is that planes shifts as it is constructed from origin and normal, not from origin and two x and y axis:
Point3d origin = lnA.PointAt(0.5);
Vector3d S = a.Normal + b.Normal;
Vector3d dL = Vector3d.Subtract((Vector3d)lnA.From, (Vector3d)lnA.To);
Vector3d BisectorN = Vector3d.CrossProduct(dL,S);
BisectorN.Unitize();
return new Plane(origin, BisectorN);
Is there a formula to average all the x, y coordinates and find the location in the dead center of them.
I have 100x100 squares and inside them are large clumps of 1x1 red and black points, I want to determine out of the red points which one is in the middle.
I looked into line of best fit formulas but I am not sure if this is what I need.
Sometimes all the red will be on one side, or the other side. I want to essentially draw a line then find the center point of that line, or just find the center point of the red squares only. based on the 100x100 grid.
List<Point> dots = new List<Point>();
int totalX = 0, totalY = 0;
foreach (Point p in dots)
{
totalX += p.X;
totalY += p.Y;
}
int centerX = totalX / dots.Count;
int centerY = totalY / dots.Count;
Simply average separately the x coordinates and the y coordinates, the result will be the coordinates of the "center".
What if there are two or more subsets of red points ? Do you want the black point inside them?
Otherwis, if I understood your question, just give a weight of 1 to red points and 0 to blacks. Then do the weighted mean on X and Y coordinate
I have a Rect A inside an enclosing Rect B. the enclosing Rect B is the area in which the Rect A is allowed to be moved. when trying to move Rect A beyond the borders of enclosing Rect B it should get stuck at a border or corner of enclosing Rect B and move no further. while moving I have the following parameters at hand: properties of enclosing Rect B, properties of Rect A before the move, potential topleft position of Rect A after the move. Note that the move is not necessarily per-pixel, it might just as well be (for example) 20 pixels, in any direction. please tell me how do I go about doing this efficiently but not over-complicated?
PS: these are simply drawn geometries on a canvas in WPF so the use of transforms is also allowed but I only have Rect variables at this particular bit, not RectangleGeometries.
eventually I created an enclosing Rect of A and B, and then applied the solution in this question, like this:
private static Point CorrectForAllowedArea(Point previousLocation, Point newLocation, Rect allowedArea, Rect newBox)
{
// get area that encloses both rectangles
Rect enclosingRect = Rect.Union(allowedArea, newBox);
// get corners of outer rectangle, index matters for getting opposite corner
var outsideCorners = new[] { enclosingRect.TopLeft, enclosingRect.TopRight, enclosingRect.BottomRight, enclosingRect.BottomLeft }.ToList();
// get corners of inner rectangle
var insideCorners = new[] { allowedArea.TopLeft, allowedArea.TopRight, allowedArea.BottomRight, allowedArea.BottomLeft }.ToList();
// get the first found corner that both rectangles share
Point sharedCorner = outsideCorners.First((corner) => insideCorners.Contains(corner));
// find the index of the opposite corner
int oppositeCornerIndex = (outsideCorners.IndexOf(sharedCorner) + 2) % 4;
// calculate the displacement of the inside and outside opposite corner, this is the displacement outside the allowed area
Vector rectDisplacement = outsideCorners[oppositeCornerIndex] - insideCorners[oppositeCornerIndex];
// subtract that displacement from the total displacement that moved the shape outside the allowed area to get the displacement inside the allowed area
Vector allowedDisplacement = (newLocation - previousLocation) - rectDisplacement;
// use that displacement on the display location of the shape
return previousLocation + allowedDisplacement;
// move or resize the shape inside the allowed area, right upto the border, using the new returned location
}