Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SierpinskiTriangleChaosGameRecursion
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Drawing; namespace Rextester { public class Program { static CheckedPoint[,] plain; static Vector2[] triangleVertices = new Vector2[3]; static Random rnd; public static void Main(string[] args) { rnd = new Random(); Ask4PlainSize: Console.Write("Enter plain size: "); int size; bool checkSize = int.TryParse(Console.ReadLine(), out size); if (!checkSize) { goto Ask4PlainSize; } plain = new CheckedPoint[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { plain[i, j] = new CheckedPoint(false, false, false, false); } } Console.WriteLine("Plain created with the size of {0}x{0}", size); triangleVertices[0] = new Vector2(size/2, (size/10)*8); triangleVertices[1] = new Vector2(size/2+(float)Math.Cos(Math.PI/6)*((size/10)*3), size/2-(float)Math.Sin(Math.PI/6)*((size/10)*3)); triangleVertices[2] = new Vector2(size/2-(float)Math.Cos(Math.PI/6)*((size/10)*3), size/2-(float)Math.Sin(Math.PI/6)*((size/10)*3)); Console.WriteLine("Triangle vertices positions are : A{0};B{1};C{2}", triangleVertices[0].ToString(), triangleVertices[1].ToString(), triangleVertices[2].ToString()); plain[(int)triangleVertices[0].x, (int)triangleVertices[0].y] = new CheckedPoint(true, true, false, false); plain[(int)triangleVertices[1].x, (int)triangleVertices[1].y] = new CheckedPoint(true, false, true, false); plain[(int)triangleVertices[2].x, (int)triangleVertices[2].y] = new CheckedPoint(true, false, false, true); bool stop = StartChaosGame(RandomizePosition(size), 0, 0); Console.WriteLine("Chaos game has done. Saving the outp image now..."); EncodeDataToPNG("SD card//DCIM//SierpinskiTriangleCheosGamerecursion//Outp//", size); Console.WriteLine("Saving has Done!!! Press any key to exit"); Console.ReadKey(); System.Diagnostics.Process.Start("SD card//DCIM//SierpinskiTriangleCheosGamerecursion//Outp//outp.png"); } static bool StartChaosGame(Vector2 pos, int vertexIndex, int attempts) { if (vertexIndex > 2 || attempts > 2) { return true; } else if (!IsInsideTriangle(new Vector2((pos.x + triangleVertices[vertexIndex].x) / 2, (pos.y + triangleVertices[vertexIndex].y) / 2)) || plain[(int)triangleVertices[vertexIndex].x, (int)triangleVertices[vertexIndex].y].CheckVertexFromIndex(vertexIndex)) { return StartChaosGame(pos, vertexIndex + 1, attempts+1); } else if (!plain[(int)pos.x, (int)pos.y].pointMarked) { plain[(int)pos.x, (int)pos.y].MarkCheckVertexFromIndex(vertexIndex, true); plain[(int)pos.x, (int)pos.y].pointMarked = true; int newIndex = rnd.Next(); return StartChaosGame(new Vector2((pos.x + triangleVertices[vertexIndex].x) / 2, (pos.y + triangleVertices[vertexIndex].y) / 2), newIndex%3, 0); } else { plain[(int)pos.x, (int)pos.y].MarkCheckVertexFromIndex(vertexIndex, true); Random rnd = new Random(); int newIndex = rnd.Next(3); return StartChaosGame(new Vector2((pos.x + triangleVertices[vertexIndex].x) / 2, (pos.y + triangleVertices[vertexIndex].y) / 2), newIndex, 0); } } static void EncodeDataToPNG (string path, int resolution) { Bitmap bmp = new Bitmap(resolution, resolution); for (int i = 0; i < resolution; i++) { for (int j = 0; j < resolution; j++) { if (plain[i,j].pointMarked) { bmp.SetPixel(i, resolution - j - 1, Color.Black); } else { bmp.SetPixel(i, resolution - j - 1, Color.White); } } } bmp.Save(path + "outp.png", System.Drawing.Imaging.ImageFormat.Png); } static Vector2 RandomizePosition(int max) { Random rnd = new Random(); return new Vector2(rnd.Next(max), rnd.Next(max)); } static bool IsInsideTriangle(Vector2 pos) { LinearLine a = new LinearLine(triangleVertices[1], triangleVertices[2]); LinearLine b = new LinearLine(triangleVertices[0], triangleVertices[2]); LinearLine c = new LinearLine(triangleVertices[0], triangleVertices[1]); return (!a.IsPointUnderLine(pos) && b.IsPointUnderLine(pos) && c.IsPointUnderLine(pos)); } } public class CheckedPoint { public bool pointMarked = false; public bool checkedA = false, checkedB = false, checkedC = false; public CheckedPoint() { pointMarked = false; checkedA = false; checkedB = false; checkedC = false; } public CheckedPoint(bool _pointMarked, bool _checkedA, bool _checkedB, bool _checkedC) { pointMarked = _pointMarked; checkedA = _checkedA; checkedB = _checkedB; checkedC = _checkedC; } public bool CheckVertexFromIndex(int index) { if (index == 1) return checkedA; else if (index == 2) return checkedB; else return checkedC; } public void MarkCheckVertexFromIndex(int index, bool res) { if (index == 1) checkedA = res; else if (index == 2) checkedB = res; else checkedC = res; } } public class Vector2 { public float x, y; public Vector2() { x = 0; y = 0; } public Vector2(float _x, float _y) { x = _x; y = _y; } public static bool Compare(Vector2 a, Vector2 b) { return a.x == b.x & a.y == b.y; } public override string ToString() { return "[" + x + "," + y + "]"; } } public class LinearLine { float a, b; public LinearLine(Vector2 p1, Vector2 p2) { a = (p1.y - p2.y) / (p1.x - p2.x); b = p1.y - a * p1.x; } public bool IsPointUnderLine(Vector2 p) { return (p.y < p.x * a + b); } } }
run
|
edit
|
history
|
help
0
dlritytio
1
OddEven
encryption
Find Prime number
Expand Treeview Job Entry
WithoutPortNo
My First Hello world
XmlException in XmlSerializer.Deserialize when XML has \0
PhoneBook- Dictionary