I've been trying to generate points along the ring of a 2D disc (both translated and rotated) in 3D space using only the disc's position and normal.
I've been using the following code to generate points and have been testing it in Matlab (but will be utilising this in c#) to check that the points are generated correctly, however it doesn't seem to be generating points correctly.
numPoints = 25;
radius = 1;
pos = [1; 2; 3];
dir = normc([3; 4; 6]); % normalised
function [pointsT, points] = GenerateDiscPoints(numPoints, radius, pos, dir)
points = zeros(numPoints, 3);
pointsT = zeros(numPoints, 3);
% Angle between points
angle = 2 * pi / numPoints;
for i = 1:numPoints+1
% Current point angle
theta = angle * i;
% Generate point in flat disc (y is vertical axis in Unity)
x = radius * cos(theta) + pos(1);
y = 0 + pos(2);
z = radius * sin(theta) + pos(3);
% Save points
points(i, 1) = x;
points(i, 2) = y;
points(i, 3) = z;
% Calculate t value to translate points
t = (dir(1) * pos(1) - dir(1) * x + dir(2) * pos(2) - dir(2) * y + dir(3) * pos(3) - dir(3) * z) / (dir(1)*dir(1) + dir(2)*dir(2) + dir(3)*dir(3));
% Translate points to correct location
xT = x + t*dir(1);
yT = y + t*dir(2);
zT = z + t*dir(3);
% Save translated points
pointsT(i, 1) = xT;
pointsT(i, 2) = yT;
pointsT(i, 3) = zT;
end
% Plot
figure;
hold all;
grid on;
scatter3(points(:,1), points(:,2), points(:,3), 25, 'r');
scatter3(pointsT(:,1), pointsT(:,2), pointsT(:,3), 25, 'g');
p3 = line([pos(1) pos(1)+dir(1)], [pos(2) pos(2)+dir(2)], [pos(3) pos(3)+dir(3)]);
set(p3, 'Color', 'blue');
end
The blue line is the normal of the disc, the red points are the points before being translated, and the green points are the points after being translated. To my eye it appears that the translated points don't seem to be generating in a disc that has the normal specified.
What's wrong with my current algorithm? What would a better way to do this be?