How to get coordinates on a circle - coordinates

I need to draw a circle by drawing line from a point to each degree of circle.
Drawing a line requires start x,y and stop x,y. So Stop x,y are the coordinates on the circle.
So how to get all coordinates on the circle.

Usually you compute a circle like
x = radius*cos(angle);
y = radius*sin(angle);
and let angle vary from 0 to 2*pi
If the circle has a center other than 0,0 then you use
x = cx+radius*cos(angle);
y = cy+radius*sin(angle);
with (cx,cy) the center coordintes.
Drawing a circle depends on your toolkit. Most graphic toolkits provide this out of the box. You just need to lookup their API.

You'll need to use DrawEllipse if you want to draw a circle using GDI+

Related

How to position and scale a triangular model by knowing its vertices in Unity3d

I want to position different equilateral triangular models, in a 3d space in unity. The problem is that the 3 known vertices aren't equilateral triangular, some of them aren't even isosceles so I need to wrap my model to match it's corners to the given vertices.
I would like to model those triangles different from each other that's why want to use pre-created models.
Currently I do the following calculation to position and scale the triangles onto a isosceles triangle:
Middle-point of the given 3 vertices
Vector3 middlepoint = (points[0]+points[1]+points[2])/3;
Distance from Middle-point
pointdistance[i] = Vector3.Distance(points[i],middlepoint);
The closest point is the one I will rotate the triangle to, so I know the triangles height (y-Axis), let's say the corner point is points[0] so float height = Vector3.Distance(points[0],middlepoint);
(I'm certain this step is wrong for a non isosceles triangle) I calculate it's width by determining the circumscribed circle radius, with the help of the remaining points
float width = (float)(Vector3.Distance(points[1] , points[2])*Math.Sqrt(3)/3);
Apply the scale to the model
float scale = new Vector3(height,width,1);
I calculate the normal normalVec of those 3 points to get the x and y orientation right, this works well so i think I don't need to change it
Instantiate the triangle
this.Triangle = (GameObject)Instantiate(standardTriangleModel,middlepoint, Quaternion.LookRotation(normalVec,points[0]));
The result looks pretty good until the triangles are not isosceles anymore
(Blue line = middlepoint to closest point, Green lines = connection between the given vertices)
So does anyone have a clue how i could position and resize my triangular models to match those points?
No code as I don't have unity handy at the moment. This answer is based on how to shear using unity gameobject transforms by trejkaz on the Unity Q&A site.
Start with gameobjects that are a right triangle of height and width 1:
Then for Triangle ABC, Set the X scale of the right triangle gameobject (which we can call mainObject) to be the length of AB, and set the Y scale to be the shortest distance between C and the line that travels through AB (the height of the triangle measured from the base AB).
Consider the angle CAB = θ.
Then, put mainObject inside of a parent gameobject called Outer1. Scale Outer1 with Y=sqrt(2)/sin(90-θ), X=sqrt(2).
Then, put Outer1 inside of a parent gameobject called Outer2. Rotate Outer2 around mainObject.forward by (θ-90) (which should be a clockwise rotation of 90-θ).
Then, put Outer2 inside of a parent gameobject called Outer3. Scale Outer3 with Y=sin((90-θ)/2), X=cos((90-θ)/2).
At this point, mainObject should be sheared and scaled into the correct shape. You will just need to position and rotate Outer3 so that the (pre-shearing) right angle corner of mainObject is at A,mainObject.right points from A to B, and mainObject.forward points normal to the triangle.

matlab: how to transform screen pixels into specific coordinates

I have to draw a curve captured on a image using screen pixels (mouse clicks) into a coordinate system. E.g.: Pixels on the screen, from left to right (130 px to 970 px) correspond to the x-axis of my coordinate system (1000 to 6000). Pixels from bottom to top (670 to 99) correspond to the y-axis of coordinate system (0 to 1.2). How can this be done? Maybe there's a function in matlab doing something like that?
Some more explanation:
I have a jpg image of a curve on a coordinate system. I've got pixel positions (x,y) of several points on that curve. Now I want to plot same curve into a matlab figure with same x and y axis as on the jpg image.
Not sure if there is a MATLAB function/command to do this, but it may not be too difficult to come up with something.
Suppose that xPixDiff = 970-130 and xAxisDiff = 6000-1000. Then the xPixel value from any (xPixel,yPixel) pair can be translated into an x-axis coordinate via
xAxisCoord = (xPixel-130)*xAxisDiff/xPixDiff + 1000
It is clear from the above that xPixel=130 maps to 1000 and xPixel=970 maps to 6000.
The yAxisCoord calculation is similar but we just have to remember that the "directions" are opposite in the y-axis coordinate system and the y pixel positions.
Let yPixDiff=99-670 and yAxisDiff=1.2-0. Then the yPixel value from any (xPixel,yPixel) pair can be translated into an y-axis coordinate via
yAxisCoord = (yPixel-670)*yAxisDiff/yPixDiff + 0
It is clear from the above that yPixel=670 maps to 0 and yPixel=99 maps to 1.2.
Hope that the above helps!

Layout dynamic number of sprites in a circle

I'd like to create a fixed size circle that will have a varying number (between 6 - 12) of rectangle sprites positioned on it. I've read about a cocos2d function called drawCircle which is great for displaying a circle. I'd like to display a circle, but I'd also like to include the rectangle sprites on top of it, spaced evenly depending on the number of sprites.
Is there a function that would layout the rectangle sprites in a circle?
I see a little bit of trigonometry in your future! Perhaps draw the circle using a drawing function, and then compute points for the center of each box?
You'll need to know the radius of your circle, obviously, but from there it should be pretty simple. It looks like you want to place them at 45 degree angles. So the first box would be placed at point (radius, 0), the second at (radius*cos(45), radius*sin(45)), third at (0, radius), etc.
The above math is assuming standard counter-clockwise rotation from 0-360 degrees. You can also use radians - you would then compute all these points with theta = 0, pi/4, pi/2, 3pi/4, pi, 5pi/4, 3pi/2, and 7pi/4
Basically is the circle center is x0, y0, your calculated points will be (x0 + radius*cos(theta), y0 + radius*sin(theta))
Should be fairly simple math at play there :)

How to rotate an image about a point that is not the image's center point using MATLAB?

What is the method to use to rotate an image about a point that is not the image's center point using MATLAB?
Two rotations of the same angle are equal up to a translation. So you can just do rotation around the center, and then translate the image to put your own center of rotation at its old position.
The help to 'rotate' says:
ROTATE Rotate objects about specified origin and direction.
ROTATE(H,[THETA PHI],ALPHA) rotates the objects with handles H
through angle ALPHA about an axis described by the 2-element
direction vector [THETA PHI] (spherical coordinates).
All the angles are in degrees. The handles in H must be children
of the same axes.
...
ROTATE(...,ORIGIN) uses the point ORIGIN = [x0,y0,y0] as the center
of rotation instead of the center of the plot box.
To rotate about a point other than the origin you:
Translate the point you want to rotate around to the origin. For example, if you want to rotate around (3,5), you would translate by (-3,-5).
Perform your rotation.
Undo the initial translation. So in my example you would now translate by (+3,+5).

MATLAB: how do I crop out a circle from an image

I need to crop a circle in MATLAB.
I need to perform iris segmentation, and I´ve identified the center point and the radius of the iris, and I need to cut it off from the image.
I have a vector ci that ci(1) is X-coordinate ci(2) is Y-coordinate and ci(3) is the radius of the circle.
One way to do this is to create a binary mask with ones inside the circle and zeros outside. You can then use this array to either mask everything outside the circle with NaNs, or to read the pixel values of the image inside the mask.
To create a circle mask, an easy way is to create coordinate arrays centered on the iris, and threshold the distance, like this:
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = (xx.^2 + yy.^2)<ci(3)^2;