mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 07:40:50 -08:00
Merge pull request #319 from drewjcooper/csharp-87-3d-plot
Add C# implementation of 87 3-D Plot
This commit is contained in:
34
87 3-D Plot/csharp/Plot.sln
Normal file
34
87 3-D Plot/csharp/Plot.sln
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26124.0
|
||||
MinimumVisualStudioVersion = 15.0.26124.0
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plot", "Plot\Plot.csproj", "{8857AE83-F481-43B0-AA51-D78E1340BD93}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Release|x64.Build.0 = Release|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{8857AE83-F481-43B0-AA51-D78E1340BD93}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
39
87 3-D Plot/csharp/Plot/Function.cs
Normal file
39
87 3-D Plot/csharp/Plot/Function.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Plot
|
||||
{
|
||||
internal static class Function
|
||||
{
|
||||
internal static IEnumerable<IEnumerable<int>> GetRows()
|
||||
{
|
||||
for (var x = -30f; x <= 30f; x += 1.5f)
|
||||
{
|
||||
yield return GetValues(x);
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<int> GetValues(float x)
|
||||
{
|
||||
var zPrevious = 0;
|
||||
var yLimit = 5 * (int)(Math.Sqrt(900 - x * x) / 5);
|
||||
|
||||
for (var y = yLimit; y >= -yLimit; y -= 5)
|
||||
{
|
||||
var z = GetValue(x, y);
|
||||
|
||||
if (z > zPrevious)
|
||||
{
|
||||
zPrevious = z;
|
||||
yield return z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int GetValue(float x, float y)
|
||||
{
|
||||
var r = (float)Math.Sqrt(x * x + y * y);
|
||||
return (int)(25 + 30 * Math.Exp(-r * r / 100) - 0.7f * y);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
87 3-D Plot/csharp/Plot/Plot.csproj
Normal file
8
87 3-D Plot/csharp/Plot/Plot.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
38
87 3-D Plot/csharp/Plot/Program.cs
Normal file
38
87 3-D Plot/csharp/Plot/Program.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
|
||||
namespace Plot
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
PrintTitle();
|
||||
|
||||
foreach (var row in Function.GetRows())
|
||||
{
|
||||
foreach (var z in row)
|
||||
{
|
||||
Plot(z);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
private static void PrintTitle()
|
||||
{
|
||||
Console.WriteLine(" 3D Plot");
|
||||
Console.WriteLine(" Creative Computing Morristown, New Jersey");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
private static void Plot(int z)
|
||||
{
|
||||
var x = Console.GetCursorPosition().Top;
|
||||
Console.SetCursorPosition(z, x);
|
||||
Console.Write("*");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user