Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Improvements #1

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions App/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
KeyDown="Page_KeyDown"
Tapped="Page_Tapped"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<Canvas x:Name="PlanetariumCanvas" Background="AliceBlue" />

<Grid KeyDown="Page_KeyDown">
<Canvas x:Name="PlanetariumCanvas" Background="AliceBlue" SizeChanged="PlanetariumCanvas_SizeChanged"/>
<CommandBar VerticalAlignment="Bottom" />
</Grid>
</Page>
116 changes: 68 additions & 48 deletions App/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Windows;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -15,9 +16,10 @@
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Media;

using Windows.System;

using PlanetsLibrary;
using PlanetsLibrary.Core;
using PlanetsLibrary.SpaceObjects;

Expand All @@ -34,19 +36,30 @@ public sealed partial class MainPage : Page {


private int Tick;
private PlanetarySystem solarSystem;
private SpaceObject CenterObject;

private int Depth = 2;
private new double Scale = 0.0005;

private double CenterX;
private double CenterY;

private DrawDelegate Draw;
private DispatcherTimer timer;

public MainPage() {
this.InitializeComponent();

this.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(this.Page_KeyDown), handledEventsToo: true);
CenterX = PlanetariumCanvas.ActualWidth / 2;
CenterY = PlanetariumCanvas.ActualHeight / 2;

// Initialize time
Tick = 0;

// Initialize solar system
solarSystem = InitializeSolarSystem();
PlanetarySystem solarSystem = new SolarSystem();
CenterObject = solarSystem.CenterObject;

// Initialize DrawDelegate
Draw = new DrawDelegate(Objects.DrawSwitch);
Expand All @@ -58,62 +71,69 @@ public MainPage() {
timer.Start();
}

private PlanetarySystem InitializeSolarSystem() {
Star sun = new Star("Sun", 25);

Planet Mercury = new Planet("Mercury", radius: 10);
Planet Venus = new Planet("Venus", radius: 20);
Planet Earth = new Planet("Earth", radius: 20);
Planet Mars = new Planet("Mars", radius: 12);

// AsteroidBelt

Planet Jupiter = new Planet("Jupiter", radius: 12);
Planet Saturn = new Planet("Saturn", radius: 12);
Planet Uranus = new Planet("Uranus", radius: 12);
Planet Neptune = new Planet("Neptune", radius: 12);

DwarfPlanet Pluto = new DwarfPlanet("Pluto", radius: 12);

Earth.AddSatelliteOrbit(
new Orbit(
new Moon("Luna", radius: 4), radius: 30, period: 100
)
);

sun.AddSatelliteOrbits(new List<Orbit>() {
new Orbit(Mercury, radius: 50, period: 100),
new Orbit(Venus, radius: 100, period: 250),
new Orbit(Earth, radius: 150, period: 365),
new Orbit(Mars, radius: 200, period: 650),
new Orbit(Jupiter, radius: 400, period: 4000),
new Orbit(Saturn, radius: 500, period: 10000),
new Orbit(Uranus, radius: 600, period: 30000),
new Orbit(Neptune, radius: 700, period: 60000),
new Orbit(Pluto, radius: 800, period: 90000),
});

return new PlanetarySystem(sun);
}

private void DispatcherTimerTick(object sender, object e) {
// Increment ticker upon timer fire
Tick++;

// Draw solar system on canvas
DrawOnCanvas(solarSystem, Tick);
DrawOnCanvas(CenterObject, Tick);
}

private void DrawOnCanvas(PlanetarySystem planetarySystem, int Tick) {
private void DrawOnCanvas(SpaceObject centerObject, int Tick) {
// Clear before new frame
PlanetariumCanvas.Children.Clear();

// Draw planetary system
centerObject.DrawSatellitesRecursively(
tick: Tick,
x: CenterX,
y: CenterY,
scale: Scale,
maxDepth: Depth,
canvas: PlanetariumCanvas,
@delegate: Draw
);
}

// Get width and height of canvas to determine center
double Width = PlanetariumCanvas.ActualWidth;
double Height = PlanetariumCanvas.ActualHeight;
public void Page_KeyDown(object sender, KeyRoutedEventArgs e) {

if (timer.IsEnabled) {
timer.Stop();
} else {
timer.Start();
}

switch (e.Key) {
case VirtualKey.Left: Scale *= 2; break;
case VirtualKey.Right: Scale /= 2; break;
case VirtualKey.Down: Depth--; break;
case VirtualKey.Up: Depth++; break;
case VirtualKey.Space: {
if (timer.IsEnabled) {
timer.Stop();
} else {
timer.Start();
}
break;
}
default: return;
}
}

// Draw planetary system
planetarySystem.DrawSystemAtTime(Tick, Width / 2, Height / 2, PlanetariumCanvas, Draw);
private void Page_Tapped(object sender, TappedRoutedEventArgs e) {
if (timer.IsEnabled) {
timer.Stop();
} else {
timer.Start();
}

Focus(FocusState.Programmatic);
}

private void PlanetariumCanvas_SizeChanged(object sender, SizeChangedEventArgs e) {
// Get width and height of canvas to determine center
CenterX = PlanetariumCanvas.ActualWidth / 2;
CenterY = PlanetariumCanvas.ActualHeight / 2;
}
}
}
32 changes: 0 additions & 32 deletions Assignment3.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "PlanetsLibrary", "PlanetsLibrary\PlanetsLibrary.shproj", "{6FEB5951-75F2-4D8D-9DC7-4F26B0223AA2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlanetsLibraryTests", "PlanetsLibraryTests\PlanetsLibraryTests.csproj", "{BBACA105-64C2-45BB-8A18-E13A20099F8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App\App.csproj", "{D66DDE2E-F570-45FE-B317-A415852D458E}"
EndProject
Global
Expand All @@ -31,36 +29,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|Any CPU.ActiveCfg = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|Any CPU.Build.0 = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|ARM.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|ARM.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|ARM64.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|ARM64.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|x64.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|x64.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|x86.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Build|x86.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|ARM.ActiveCfg = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|ARM.Build.0 = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|ARM64.Build.0 = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|x64.ActiveCfg = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|x64.Build.0 = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|x86.ActiveCfg = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Debug|x86.Build.0 = Debug|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|Any CPU.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|ARM.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|ARM.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|ARM64.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|ARM64.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|x64.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|x64.Build.0 = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|x86.ActiveCfg = Release|Any CPU
{BBACA105-64C2-45BB-8A18-E13A20099F8F}.Release|x86.Build.0 = Release|Any CPU
{D66DDE2E-F570-45FE-B317-A415852D458E}.Build|Any CPU.ActiveCfg = Release|x64
{D66DDE2E-F570-45FE-B317-A415852D458E}.Build|Any CPU.Build.0 = Release|x64
{D66DDE2E-F570-45FE-B317-A415852D458E}.Build|Any CPU.Deploy.0 = Release|x64
Expand Down
6 changes: 3 additions & 3 deletions PlanetsLibrary/Core/Orbit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public ValueTuple<double, double> RelativePositionFromTime(int tick) {
return (X, Y);
}

public ValueTuple<double, double> RelativePositionFromTime(int tick, double aroundX, double aroundY) {
public ValueTuple<double, double> RelativePositionFromTime(int tick, double aroundX, double aroundY, double scale = 1) {
ValueTuple<double, double> RelativePosition = this.RelativePositionFromTime(tick);

double X = RelativePosition.Item1 + aroundX;
double Y = RelativePosition.Item2 + aroundY;
double X = RelativePosition.Item1 * scale + aroundX;
double Y = RelativePosition.Item2 * scale + aroundY;

return (X, Y);
}
Expand Down
4 changes: 2 additions & 2 deletions PlanetsLibrary/Core/PlanetarySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public PlanetarySystem(SpaceObject centerObject) {
CenterObject = centerObject;
}

public void DrawSystemAtTime(int time, double centerX, double centerY, Canvas canvas, DrawDelegate @delegate = null) {
CenterObject.DrawSatellitesRecursively(time, centerX, centerY, canvas, @delegate);
public void DrawSystemAtTime(int time, double centerX, double centerY, double scale, int maxDepth, Canvas canvas, DrawDelegate @delegate = null) {
CenterObject.DrawSatellitesRecursively(time, centerX, centerY, scale, maxDepth, canvas, @delegate);
}
}
}
22 changes: 13 additions & 9 deletions PlanetsLibrary/Core/SpaceObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ public class SpaceObject {

public String Name { get; set; }
public String Color { get; set; }
public double Radius { get; set; }

// protected SpaceObject OrbitingAround { get; set; }
public double Radius { get; set; }

public List<Orbit> SatelliteOrbits { get; set; }

Expand All @@ -22,21 +21,26 @@ public SpaceObject(String name, double radius) {
Radius = radius;
}

public virtual void Draw(double x, double y, Canvas canvas, DrawDelegate @delegate = null) {
public virtual void Draw(double x, double y, double scale, Canvas canvas, DrawDelegate @delegate = null) {
if (@delegate != null) {
@delegate.DynamicInvoke(this, canvas, x, y);
} else {
@delegate.Invoke(this, canvas, x, y);
}
else {
Console.WriteLine(Name + " X: " + x + " Y: " + y);
}
}

public void DrawSatellitesRecursively(int tick, double x, double y, Canvas canvas, DrawDelegate @delegate) {
this.Draw(x, y, canvas, @delegate);
public void DrawSatellitesRecursively(int tick, double x, double y, double scale, int maxDepth, Canvas canvas, DrawDelegate @delegate) {
if (maxDepth < 1) {
return;
}

this.Draw(x, y, scale, canvas, @delegate);

foreach (Orbit orbit in this.SatelliteOrbits) {
ValueTuple<double, double> SatellitePosition = orbit.RelativePositionFromTime(tick, x, y);
ValueTuple<double, double> SatellitePosition = orbit.RelativePositionFromTime(tick, x, y, scale);

orbit.Satellite.DrawSatellitesRecursively(tick, SatellitePosition.Item1, SatellitePosition.Item2, canvas, @delegate);
orbit.Satellite.DrawSatellitesRecursively(tick, SatellitePosition.Item1, SatellitePosition.Item2, scale, maxDepth - 1, canvas, @delegate);
};
}

Expand Down
1 change: 1 addition & 0 deletions PlanetsLibrary/PlanetsLibrary.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Core\Orbit.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\SpaceObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\PlanetarySystem.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SolarSystem.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Core\" />
Expand Down
Loading