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.
Related
I am working on a mobile app in C# using the Xamarin framework. I am trying to move a point by a fixed angle on a map like in the first part of the gif below. I believe I am using the right mathematical functions to compute the coordinates of the shifted points since in first part of the GIF, in GeoGebra, everything seems to be fine.
But when it comes to the actual in-app implementation, the results are quite weird : the angle is not consistent and the distance between the center and the points varies by moving the target.
The GIF showing the issue
I don't have a clue about what is wrong with the code. In the code below I use polylineOptions to draw the lines but I've tried with a Polygon and it displays the same results. Maybe it's because customMap.UserPin.Position returns the coordinates in Decimal Degree format (i.g. 34.00462, -4.512221) and the gap between two position is too small for a double.
Here are the two functions used to draw the lines.
// Add a cone's side to the variable coneLines
private void addConePolyline(double angle, CustomMap customMap, LatLng userPos)
{
// The coordinates of the end of the side to be drawn
LatLng conePoint = movePoint(angle, customMap.UserPin.Position, customMap.TargetPin.Position);
var polylineOptions = new PolylineOptions();
polylineOptions.InvokeWidth(10f);
polylineOptions.InvokeColor(Android.Graphics.Color.Argb(240, 255, 20, 147)); // Pink
polylineOptions.Add(userPos);
polylineOptions.Add(conePoint);
// Add the line to coneLines
coneLines.Add(map.AddPolyline(polylineOptions));
}
// Moves a point by the given angle on a circle of center rotationCenter with respect to p
private LatLng movePoint(double angle, Position rotationCenter, Position initialPoint)
{
// Compute the components of the translation vector between rotationCenter and initialPoint
double dx = initialPoint.Latitude - rotationCenter.Latitude;
double dy = initialPoint.Longitude - rotationCenter.Longitude;
// Compute the moved point's position
double x = rotationCenter.Latitude + Math.Cos(angle) * dx - Math.Sin(angle) * dy;
double y = rotationCenter.Longitude + Math.Sin(angle) * dx + Math.Cos(angle) * dy;
LatLng res = new LatLng(x, y);
return res;
}
I hope someone can help me with this!
Thank you.
I currently have a code to generate a 2D array into GameObjects, my currently properties are:
Width: 1920
Height: 1080
Camera Size: 540
2D Array size: 30x16
Each block represents 64x64 pixels
My problem is that I position my blocks from -960 to 960 (1920 width) and -540 to 540. But when I do that, the block that was supposed to be at the first position gets wrongly put on:
So from the vision of the camera, probably most people would figure out that I have to put half of the block width added to its x position, and half of blocks height decreased from its y position, but when I try that, this occour:
Code:
float height = Camera.main.orthographicSize *2f;
float width = height / (float)Screen.height * (float)Screen.width;
Destroy(map);
map = new GameObject();
print("W: "+width+". H: "+height);
lastH=height;
lastW=width;
for (int i=0; i<30; i++) {
for (int j=0; j<16; j++) {
if(mapa[i,j]>0){
GameObject aux = objetos[mapa[i,j]-1];
float wX=aux.transform.localScale.x,hY=aux.transform.localScale.y;
print ("wX: "+wX+", hY: "+hY);
float unidadeW = 2*(float)lastW/(float)(20*wX);
float unidadeH = 2*(float)lastH/(float)(20*hY);
//aux.transform.localScale = new Vector3(unidadeW,unidadeH,0);
GameObject t = (GameObject)Instantiate(aux, new Vector2(j*wX*0.64f-width/2,-i*hY*0.64f+height/2),Quaternion.identity);
t.transform.parent = map.transform;
}
}
}
OBS: There are several stuff that I don't use anymore because i'm changing the code, so don't mid it.
And I ask, WHYYYY?
The correct way to make a distance between two units is their diagonal, see the image below:
Doesn't matter which is your orientation point, if its top left or center, the distance between the blocks will be √2*side, so the correct distance between mine was around 22, and I can find it by doing this equation:
sqrt(2)/2*32=22.62 (the correct distance between two tiles of 64x64), instead of using 64+ at the last x and 64+ at the last y the right way to do is by adding 22.62 at both multiplications.
I have images from two camera. Both of them send a picture but the picture of camera 1 is zoomed(Its like that picture of camera 1 is inside of picture of camera 2).
I have the position of a point in picture of camera 1. This position can change in different pictures. Now I want to find that point in picture of camera 2.
Both of cameras images are in 2560X2048 px.
How can I find that x,y in picture 2?
I found the answer. I cropped the unzoomed! picture to be equal to zoomed picture. and save the x,y of cropped picture on unzoomed picture. Than I calculate percent of x,y of that point in zoomed picture. something like this :
double percentXZoom = (I_PLATE_MIN_X * 100) / 2560;
double xCropedImage = xD - xU;
double xDiff = (xCropedImage * percentXZoom) / 100;
double x = xD + Math.Abs(xDiff);
double percentYZoom = (I_PLATE_MIN_Y * 100) / 2560;
double yCropedImage = yD - yU;
double yDiff = (yCropedImage * percentYZoom) / 100;
double y = yD + Math.Abs(yDiff);
"2560" is the size of pixel in pictures.
xD, yD is the start point of cropped picture. and xU, yU is the end point of cropped picture.
Now I have the x,y of that point in unzoomed picture.
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 am trying to draw a rectangular object that allows the user to click on a corner-point to resize and also rotate the rectangle in a 2D space.
Therefore I am using an array of four points ordered A, B, C, D (or 0, 1, 2, 3) from top-left to bottom-left in clockwise order.
The rotation works fine, I calculate the center point and rotate each point around it by an angle.
The resizing is done by determining which point was pressed down, and then setting its new position to the position of the mouse on each MouseMove event. The two adjacent points then need to be updated to stay in a rectangular shape. The resizing is intermittently not working. I have tried many ways to error-check, but all leave me with the same problem where if I move the mouse back and forth over the opposing point while moving a point, the points get distorted and are no longer a rectangular shape.
SOURCE CODE HERE
https://www.assembla.com/code/moozhe-testing/subversion/nodes/rotateRectangle
EXERPT OF PROBLEM CODE
private void MovePoint(int id, PointF newPoint)
{
PointF oldPoint = points[id];
PointF delta = newPoint.Substract(oldPoint);
PointF pointPrevious = points[(id + 3) % 4];
PointF pointNext = points[(id + 1) % 4];
PointF sidePrevious = pointPrevious.Substract(oldPoint);
PointF sideNext = pointNext.Substract(oldPoint);
PointF previousProjection = Projection(delta, sidePrevious);
PointF nextProjection = Projection(delta, sideNext);
pointNext = pointNext.AddPoints(previousProjection);
pointPrevious = pointPrevious.AddPoints(nextProjection);
points[(id + 3) % 4] = pointPrevious;
points[(id + 1) % 4] = pointNext;
points[id] = newPoint;
}
private PointF Projection(PointF vectorA, PointF vectorB)
{
PointF vectorBUnit = new PointF(vectorB.X, vectorB.Y);
vectorBUnit = vectorBUnit.Normalize();
float dotProduct = vectorA.X * vectorBUnit.X + vectorA.Y * vectorBUnit.Y;
return vectorBUnit.MultiplyByDecimal(dotProduct);
}
It sounds like you might want to be using a transformation matrix, instead of updating X/Y coordinates manually. Please check out this link:
Comparing GDI mapping modes with GDI+ transforms
Here's the MSDN reference:
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.transform