sexta-feira, 31 de julho de 2015

Fazendo uma "lua" girar - simulação no Scilab


O código abaixo (que eu não escrevi) gera um sistema 'planeta + lua' no qual a lua fica orbitando o planeta.

//--------------------------------------------------
clear;clc;clf;

// **** SUBOUTINE **** //
// Attach defined points to the spheres:
function [x, y, z]=facet(v, h)
x = cos(v)'*cos(h); // Facet x-matrix
y = cos(v)'*sin(h); // Facet y-matrix
z = sin(v)'*ones(h); // Facet z-matrix
endfunction

// **** MAIN **** //
// Define planet & moon variables:
//--------------------------------------------------
// Planet (p), 10x10 degree grid:
vp = linspace(-%pi/2,%pi/2,18); // 18 steps vertically
hp = linspace(0,2*%pi,36); // 36 steps horizontally
rp = 2;

// Moon (m), 20x20 degree grid & offset from origin:
vm = linspace(-%pi/2,%pi/2,9); // 9 steps vertically
hm = linspace(0,2*%pi,18); // 18 steps horizontally
rm = 0.3; // Moon radius
Rm = 2.1; // Moon offset
Az = 0; // Moon start point
n = 1; // # of moon revolutions
step = 100 // # of steps/revolution

// Define facets for spheres using subroutine facet():
//--------------------------------------------------
[Xp,Yp,Zp] = facet(vp,hp); // Planet
[Xm,Ym,Zm] = facet(vm,hm); // Moon

// Plot commands (box, planet, moon):
//---------------------------------------------------
// Define 3D box, put double buffer on, define surface:
a = gca();
a.data_bounds = [-5,-5,-3; 5,5,3]; // 3D box size
f = gcf();
title('Planeta + Lua');
//f.pixmap = "on"; // Double buffer
f.color_map = hotcolormap(32); // Surface color

// Plot planet & rotating moon:
for Az = 0 : 2*%pi/step : n*2*%pi
    
    // Delete previous entities (planet & moon):
    if (a.children~=[]) then // IF frame contains graph...
        delete(a.children); // then delete graph
    end
    
    // Plot planet & define facet edges:
    a.background = color('grey'); // Box wall color
    surf(rp*Xp, rp*Yp, rp*Zp); // Plot planet
    e1 = gce();
    e1.foreground = color('red'); // Facet edge color
    
    // Plot moon & define facet edges:
    x_loc = Rm*sin(Az); // Location on x axis
    y_loc = Rm*cos(Az); // Location on y axis
    C = Rm*[x_loc, -y_loc, 0] // Moon center
    surf(C(1)+rm*Xm, C(2)+rm*Ym, C(3)+rm*Zm); // Plot moon
    e2 = gce();
    e2.foreground = color('blue'); // Facet edge color
end
// **** END MAIN **** //
 

Nenhum comentário:

Postar um comentário