Very Difficult MATLAB program I am trying to write.
Afficher commentaires plus anciens
I am trying to simulate the phases of the moon. I want to give a representation for the 28 days of the moon cycle (see link ). I am wanting to tackle this problem by using rectangles to shade in the light parts that represent the part of the moon showing. How would I do this if I already know how to physically draw the rectangles, I just dont know how to position or make them the right size?? I have been thinking for hours. Please help.
6 commentaires
Image Analyst
le 14 Oct 2011
Your link didn't make it. Maybe you should think about ellipses instead.
Brian
le 14 Oct 2011
Walter Roberson
le 14 Oct 2011
The function call to draw ellipses is the same as the function call to draw rectangles: both involve calls to rectangle(). And it is probably easy to figure out the space occupied by a rectangle than to figure out all the intersection points near a set of ellipses.
Image Analyst
le 14 Oct 2011
I was thinking of only one ellipse, placed at the center, with major axis the same as the moon and then changing the minor axis from 0 to the moon radius, and taking only half of it - if you can visualize that somewhat bad description. The other half would be then either be the lit moon, or black (the dark moon). I could be wrong though - I'm not sure of the exact equation of the shadow.
Walter Roberson
le 14 Oct 2011
Trying to hack together some of the science of orbital mechanics...
The shadow on the moon should instantaneously follow a Great Circle on the moon, where the pole of the Great Circle would be deemed to be normal to the direction to the Sun. I say "instantaneously" because that polar direction is not the same as the actual axial tilt of the moon.
Neither the bright area nor the dark area would form ellipses: for example when less than half of the moon was lit, then the bright area would have corners, one at the top and one at the bottom -- polar wedges.
There is an additional complication, which is that the Moon is inclined approximately 5 degrees in orbit relative to the Earth, so the normal between it and the Sun will differ from Earth's. We thus will not see a completely upright wedge: we observe slightly rotated from that; in particular in the Northern Hemisphere we get to peak at the "bottom" of the Moon a bit more than would naively be expected.
James Tursa
le 7 Sep 2017
@IA: Yes, the terminator ("equation of the shadow") is a section of an ellipse since it is just a circle rotated in 3D space. Your approach should work. E.g., just generate a set of bounding points, rotate the points appropriately, and a couple of "fill" commands should do it. I have no idea what the rectangle approach is supposed to do.
Réponses (2)
James Tursa
le 7 Sep 2017
Modifié(e) : James Tursa
le 7 Sep 2017
If you just want to plot a rotated moon phase, here is an approach using the "fill" command.
% ang = angle to rotate counter-clockwise (degrees)
% f = phase fraction (full moon = -1 <= f <= 1 = new moon)
function moonphaseplot(ang,f)
% Moon outer edge
a = 0:360;
xm = cosd(a);
ym = sind(a);
% Moon outer edge of lit portion
a = -90:90;
xe = cosd(a);
ye = sind(a);
% Moon terminator
a = 90:-1:-90;
xt = cosd(a)*f;
yt = sind(a);
% Rotate
R = [cosd(ang) -sind(ang);
sind(ang) cosd(ang)];
XY = R * [xm;ym];
xm = XY(1,:);
ym = XY(2,:);
XY = R * [xe xt;ye yt];
xet = XY(1,:);
yet = XY(2,:);
% Plot
figure;
hold on
z = 2;
fill([-z z z -z],[-z -z z z],'b'); % background blue sky
plot(2*z*rand(100,1)-z,2*z*rand(100,1)-z,'w.'); % background stars white
fill(xm,ym,'k'); % moon full disk black
fill(xet,yet,'w'); % moon lit portion white
axis square
axis off
title('Moon Phase')
end
For example:
>> moonphaseplot(135,-.5)

%
Walter Roberson
le 14 Oct 2011
0 votes
For any one area that you want to shade, find the largest rectangle that will fit within it, and draw that rectangle. This will touch the perimeter of the area in at least two places, thus partitioning the original area in to a set of areas. For each area so produced, find and draw the largest rectangle in what remains, found what is left over, add it to the queue. Keep going this way until your spaces all happen to be filled completely by rectangles or your remaining spaces reach single pixels (in which case you fill them all with single-pixel rectangles.) You can do the shading by requesting in the rectangle() call that the rectangle be filled.
Are there easier methods to do the shading? Yes, but they don't involve using rectangles.
6 commentaires
Brian
le 14 Oct 2011
Déplacé(e) : Dyuman Joshi
le 18 Nov 2023
Walter Roberson
le 14 Oct 2011
Déplacé(e) : Dyuman Joshi
le 18 Nov 2023
In accordance with what I said above about doing rectangles, except that instead of finding the "largest rectangle that would fit within", you would find "the largest thin rectangle that would fit within" (for whatever you deem a "thin" rectangle to be.)
Figuring out where the boundary edge for the shading should be much more difficult than doing the shading.
Brian
le 14 Oct 2011
Déplacé(e) : Dyuman Joshi
le 18 Nov 2023
Walter Roberson
le 14 Oct 2011
Déplacé(e) : Dyuman Joshi
le 18 Nov 2023
Either I do not understand or else that arrangement would lead to highly unrealistic shading.
Jan
le 14 Oct 2011
Déplacé(e) : Dyuman Joshi
le 18 Nov 2023
It would be easier to implement, if the moon is assumed to be a cube.
Walter Roberson
le 20 Nov 2023
Consider a spherical cow
Catégories
En savoir plus sur Earth and Planetary Science dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!