Draw a triangle in image knowing its center of gravity

3 views (last 30 days)
i need to draw a white triangle in a black image. So the first idea that i had got is to define the three peaks that define the triangle and then recuperate the points that belong to its surface. and then affect the value 1 to these points in a black image. To recuperate the points belong to the surface of the triangle i write this code which reflects the idea that a point belongs to a plan it verifies an equation system like after: ( the plane will be the triangle)
if true
k=1;
for i=1:size_image_x
for j=1:size_image_y
equation_x=i-x_peak1==xk*(x_peak2-x_peak1)+yk*(x_peak3-x_peak1);
equation_y=j-y_peak1==xk*(y_peak2-y_peak1)+yk*(y_peak3-y_peak1);
eqx=solve(equation_x,xk,yk);
eqy=solve(equation_y,xk,yk);
if isempty(eqx)~=1 && isempty(eqy)~=1
point_x(1,k)=i;
point_y(1,k)=j;
k=k+1;
end
end
end
end
but it doesn't work
may any one help me please
thanks

Accepted Answer

Image Analyst
Image Analyst on 17 Apr 2015
Way too complicated. Simply use poly2mask() and do it all in one line of code:
mask = poly2mask([x_peak1, x_peak2, x_peak3], [y_peak1, y_peak2, y_peak3], size_image_y, size_image_x);
  2 Comments
jan
jan on 17 Apr 2015
okay thanks a lot it is a very good and simple solution you are right. do you have any other solution if we don't have the three peaks of the triangle but we have its center of gravity????
Image Analyst
Image Analyst on 17 Apr 2015
You need something else, like the radius and angle to one of the vertices or something. Obviously you could just spin a triangle around the center of gravity and they would all be triangles, just at different orientations. It's just a matter of simple trigonometry to get the three vertices once you have enough other information. What orientation do you want to draw it at? Surely you must have SOME idea.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!