Reputation: 143
I'm in a situation that I have a point Vector3(x,y,z), and I need from that starting point to generate a circle in 3D (a ring) that starts with that point and ends with it. Right now I can generate a circle of vector3, but I don't know how to generate it using just one point. For example the main point is Red, and others are points are in black:
Upvotes: 0
Views: 3023
Reputation: 1
C# using GeoDesy and NetTopologySuite.
static Geometry Circle(double lat, double lon, double meters)
{
GeodeticCalculator geoCalc = new GeodeticCalculator(Ellipsoid.WGS84);
GlobalCoordinates start = new GlobalCoordinates(new Angle(lat), new Angle(lon));
int sides = 4;
int degree_unit = 360 / sides;
var c = new List<NetTopologySuite.Geometries.Coordinate>();
for (int i = 0; i < sides; i++)
{
GlobalCoordinates dest = geoCalc.CalculateEndingGlobalCoordinates(start, new Angle(degree_unit * i), meters);
c.Add(new NetTopologySuite.Geometries.Coordinate(dest.Longitude.Degrees, dest.Latitude.Degrees));
}
c.Add(c[0]);
GeometryFactory gc = new GeometryFactory(new PrecisionModel(), 4326);
var box = gc.CreatePolygon(c.ToArray());
var mbc = new MinimumBoundingCircle(box);
return mbc.GetCircle();
}
Upvotes: 0
Reputation: 4692
Heres an example of how to calculate circle points with a given center vector and a radius.
This is for a plane, but you could rotate them using another rotation-matrix.
class Program
{
static void Main(string[] args)
{
int n = 22; //the number of points
float radius = 22f;
Vector3 vector = new Vector3(radius, 0, 0); //the vector you keep rotating
Vector3 center = new Vector3(33,33,33); //center of the circle
float angle = (float)Math.PI * 2 / (float)n;
Vector3[] points = new Vector3[n];
Matrix rotation = Matrix.RotationZ(angle);
for (int i = 0; i < n; i++)
{
points[i] = vector + center;
vector.TransformNormal(rotation);
}
}
}
I used the managed directX framework for the project ... not sure what you use
Edit
Schematic about the rotation and translation problem:
Upvotes: 1
Reputation: 13763
To define a circle, whether it's in a 2D or a 3D space, you need three separate points that lie on the circle.
Otherwise, there are countless (mathematically infinite) possibilities.
If you only have one point, where on the circle (top, bottom, ...) is it located? What is the radius of the circle? Given that it's a 3D space, what is its inclination? You can't derive that from a single (x,y,z,) point.
Edit I made a small visualization in Paint. Note that this is a 2D plane, but the same applies to a 3D space (the three points just need to be coplanar, which any set of three points by definition are)
Edit 2
This type of question may have been better to ask on a geometry or game development (as I suspect that's what you're doing) related StackExchange site, since the question is really a geometrical issue, not a coding one.
Edit 3
I'm not going to write the code for you, as "please write my code" isn't the intention of StackOverflow.
However, there are resources online that will help with the algebraic derication of a circle, you just need to put it in code.
According to this earlier answer on SO, you can find a very detailed explanation of circle-related formulae on this reference page. Look for "Equation of a Circle from 3 Points (2 dimensions)".
Edit 4, final edit
As you asked, you can define a circle based on two points if one of those points is the center point of the circle.
The radius of the circle can then be calculated from the distance between those two points.
From then on, if you have the center point and circle radius, you can calculate any point on the circle. Please refer to the same reference link I mentioned above, as this page contains a lot of ways to derive circles from different sent of data.
Edit 5, really the final edit this time :)
This StackOverflow answer explains what you want to do in code. It assumes you know the center point and the radius, which you can derive as I mentioned above.
I hope this is enough information to help you further :)
Upvotes: 1