Reputation: 89
I have an array with points that creates a curve. Then i create another two arrays: left and right, which are supposed to be borders of my middle curve (they are modified only by +/- x'es), like this:
At the end i want to have something like this:
I tried doing that by rotating left and right point around center point (from red line) but it doesnt work well for me.
static Point Rotate(Point p, Point o, double theta)
{
Point rotated = new Point();
double x = Math.Cos(theta) * (p.X - o.X) - Math.Sin(theta) * (p.Y - o.Y) + o.X;
rotated.X = (int)Math.Round(x);
double y = Math.Sin(theta) * (p.X - o.X) + Math.Cos(theta) * (p.Y - o.Y) + o.Y;
rotated.Y = (int)Math.Round(y);
return rotated;
}
I count angle between points from red line.
float xDiff = finalList[finalList.IndexOf(row) + 1][0].x - point.toPoint().X;
float yDiff = finalList[finalList.IndexOf(row) + 1][0].y - point.toPoint().Y;
angle = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
angle = Math.Abs(angle) - 90d;
How to get that left and right point array properly? Maybe there is another way to draw curve with customizable borders? I need this to draw a chart. Any help will be appreciated :)
edit: Angles that i get seems to be ok, about 10 degrees at the bottom of line and then raising to about 50 and decreasing again so I think the problem is in rotate itself.
Upvotes: 3
Views: 566
Reputation: 22073
I've done this ones. I've created a tube arround a 3d line.
The method I used is:
Treat each point (red line) as a vector from the current point to the next point. The points on the black lines are perpendicular to this vector. (see the image below) This way you generate the points on the black line. No rotation involved.
I've added an image, to visualize my solution. The red arrows are the initial vectors of the current and the next points. The black arrows show the generated parallel vectors. The green arrows are the perpendicular vectors on the red arrows. The length of the perpendicular vectors can be used to narrow the 'parallel' line.
To calculate the perpendicular vector, use the cross product.
For my solutions, I calculated 2d circlepoints on the perpendicular plane (so they became 3d) of the current/next vector3d. Repeated that for each point and connected the, on the same angle, circlepoints to the other circlepoints and created faces. The radius of the circle was used to make the tube more widen or narrow.
Upvotes: 3