Created
January 14, 2018 12:28
-
-
Save KvanTTT/b16aea91be0ed63806cc163e271e078e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.Threading; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
namespace Powers | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
udRadius.Value = 2.0; | |
} | |
public int Factorial(int X) | |
{ | |
int Result = 1; | |
for (int i = 2; i <= X; i++) | |
Result *= i; | |
return Result; | |
} | |
public double TriangPower(double X, double Y) | |
{ | |
//return alglib.gammafunction(X + Y) / (alglib.gammafunction(X) * alglib.gammafunction(Y + 1)); | |
return 1 / (Y * alglib.beta(X, Y)); | |
} | |
public UInt64 TriangPower(UInt64 X, UInt64 Y) | |
{ | |
UInt64 Result = 1; | |
UInt64 Fact = 1; | |
for (UInt64 i = 1; i <= Y; i++) | |
{ | |
Result *= X + i - 1; | |
Fact *= i; | |
} | |
return Result / Fact; | |
} | |
public double TriangPower(double X, UInt64 Y) | |
{ | |
double Result = 1; | |
for (UInt64 i = 1; i <= Y; i++) | |
Result *= (X + i - 1) / i; | |
return Result; | |
} | |
public double TriangPower(UInt64 X, double Y) | |
{ | |
return TriangPower(Y, X) * X / Y; | |
} | |
public double Tetration(double X, int Y) | |
{ | |
if (Y == -1) | |
return 0; | |
if (Y == 0) | |
return 1; | |
double Result = X; | |
for (int i = 1; i < Y; i++) | |
Result = Math.Pow(X, Result); | |
return Result; | |
} | |
public double Tetration2(double X) | |
{ | |
if (X > 0) | |
return Math.Pow(X, X); | |
else | |
if (X == 0) | |
return 1; | |
else | |
return double.NaN; | |
} | |
private void button1_Click(object sender, RoutedEventArgs e) | |
{ | |
textBox4.Text = TriangPower(double.Parse(textBox1.Text), double.Parse(textBox2.Text)).ToString(); | |
} | |
double TriangCircle(double X, double Y, double R) | |
{ | |
return TriangPower(X, 2) + TriangPower(Y, 2) - TriangPower(R, 2); | |
} | |
double TetratCircle(double X, double Y, double R) | |
{ | |
return Tetration2(X) + Tetration2(Y) - Tetration2(R); | |
} | |
double TriangCircleSqrt(double X, double R) | |
{ | |
return TriangPower(R, 2) - TriangPower(X, 2); | |
} | |
//double[,] PlotGraphic(double Left, double Right, ) | |
double Epsilon = 0.01; | |
void PlotGraphic() | |
{ | |
if (canvasGraphic.RenderSize.Width == 0.0) | |
return; | |
canvasGraphic.Children.Clear(); | |
double Left = double.Parse(tbLeft.Text); | |
double Right = double.Parse(tbRight.Text); | |
double Top = double.Parse(tbTop.Text); | |
double Bottom = double.Parse(tbBottom.Text); | |
double Radius = (double)udRadius.Value; | |
int Width = (int)canvasGraphic.RenderSize.Width; | |
int Height = (int)canvasGraphic.RenderSize.Height; | |
double StepX = (Right - Left) / canvasGraphic.RenderSize.Width; | |
double StepY = (Top - Bottom) / canvasGraphic.RenderSize.Height; | |
double X = Left, Y = Bottom; | |
byte[] Pixels = new byte[Width * Height * 4]; | |
/*double MaxError = Math.Sqrt(StepX * StepX + StepY * StepY); | |
for (int j = Height - 1; j >= 0; j--) | |
{ | |
X = Left; | |
for (int i = 0; i < Width; i++) | |
{ | |
try | |
{ | |
double Error = Math.Abs(TriangCircle(X, Y, Radius)); | |
if (Error < MaxError) | |
{ | |
byte C = (byte)Math.Round((MaxError - Error) / MaxError * 255); | |
int ind = (Width * j + i) * 4; | |
Pixels[ind + 0] = C; | |
Pixels[ind + 1] = C; | |
Pixels[ind + 2] = C; | |
Pixels[ind + 3] = 255; | |
} | |
} | |
finally | |
{ | |
X += StepX; | |
} | |
} | |
Y += StepY; | |
}*/ | |
double[,] Values = new double[Width, Height]; | |
if ((bool)cbParallel.IsChecked) | |
{ | |
Stopwatch s = new Stopwatch(); | |
s.Start(); | |
Parallel.For(0, Width, i => | |
{ | |
double X1 = Left + StepX * i; | |
double Y1 = Bottom; | |
for (int j = Height - 1; j >= 0; j--) | |
{ | |
try | |
{ | |
Values[i, j] = TetratCircle(X1, Y1, Radius); | |
} | |
finally | |
{ | |
Y1 += StepY; | |
} | |
} | |
}); | |
s.Stop(); | |
tbTimeParallel.Text = s.ElapsedMilliseconds.ToString(); | |
} | |
else | |
{ | |
Stopwatch s = new Stopwatch(); | |
s.Start(); | |
X = Left; | |
for (int i = 0; i < Width; i++) | |
{ | |
Y = Bottom; | |
for (int j = Height - 1; j >= 0; j--) | |
{ | |
try | |
{ | |
Values[i, j] = TetratCircle(X, Y, Radius); | |
} | |
finally | |
{ | |
Y += StepY; | |
} | |
} | |
X += StepX; | |
} | |
s.Stop(); | |
tbTime.Text = s.ElapsedMilliseconds.ToString(); ; | |
} | |
int MaxIntersectCount = 0; | |
Y = Bottom; | |
double Gray8 = 255.0 / 8.0; | |
for (int j = Height - 2; j >= 1; j--) | |
{ | |
X = Left; | |
for (int i = 1; i < Width - 1; i++) | |
{ | |
try | |
{ | |
int IntersectCount = 0; | |
double Value = Values[i, j]; | |
if (Values[i - 1, j - 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i, j - 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i + 1, j - 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i + 1, j] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i + 1, j + 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i, j + 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i - 1, j + 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i - 1, j] * Value <= 0.0) | |
IntersectCount++; | |
if (IntersectCount > MaxIntersectCount) | |
MaxIntersectCount = IntersectCount; | |
Pixels[(Width * j + i) * 4] = (byte)IntersectCount; | |
/* | |
int ind = ; | |
byte ResultColor = (byte)Math.Round(IntersectCount * Gray8); | |
Pixels[ind] = ResultColor; | |
Pixels[ind + 1] = ResultColor; | |
Pixels[ind + 2] = ResultColor; | |
Pixels[ind + 3] = ResultColor;*/ | |
} | |
finally | |
{ | |
X += StepX; | |
} | |
} | |
Y += StepY; | |
} | |
double WhiteColor = 255.0 / MaxIntersectCount; | |
for (int i = 0; i < Pixels.Length; i += 4) | |
{ | |
byte ResultColor = (byte)Math.Round(Pixels[i] * WhiteColor); | |
Pixels[i] = Pixels[i + 1] = Pixels[i + 2] = Pixels[i + 3] = ResultColor; | |
} | |
/* | |
double[,] Values = new double[Width, Height]; | |
for (int j = Height - 1; j >= 0; j--) | |
{ | |
X = Left; | |
for (int i = 0; i < Width; i++) | |
{ | |
try | |
{ | |
Values[i, j] = TetratCircle(X, Y, Radius); | |
} | |
finally | |
{ | |
X += StepX; | |
} | |
} | |
Y += StepY; | |
} | |
Y = Bottom; | |
double Gray4 = 255.0 / 4.0; | |
for (int j = Height - 2; j >= 1; j--) | |
{ | |
X = Left; | |
for (int i = 1; i < Width - 1; i++) | |
{ | |
try | |
{ | |
int IntersectCount = 0; | |
double Value = Values[i, j]; | |
if (Values[i - 1, j] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i, j - 1] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i + 1, j] * Value <= 0.0) | |
IntersectCount++; | |
if (Values[i, j + 1] * Value <= 0.0) | |
IntersectCount++; | |
int ind = (Width * j + i) * 4; | |
byte ResultColor = (byte)Math.Round(IntersectCount * Gray4); | |
Pixels[ind] = ResultColor; | |
Pixels[ind + 1] = ResultColor; | |
Pixels[ind + 2] = ResultColor; | |
Pixels[ind + 3] = ResultColor; | |
} | |
finally | |
{ | |
X += StepX; | |
} | |
} | |
Y += StepY; | |
} | |
*/ | |
BitmapSource S = BitmapSource.Create(Width, Height, 96, 96, PixelFormats.Bgr32, null, Pixels, Width * 4); | |
Image I = new Image(); | |
I.Source = S; | |
canvasGraphic.Children.Add(I); | |
} | |
private void Window_Loaded(object sender, RoutedEventArgs e) | |
{ | |
} | |
private void button2_Click(object sender, RoutedEventArgs e) | |
{ | |
PlotGraphic(); | |
} | |
private void udRadius_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e) | |
{ | |
if (tbLeft != null) | |
{ | |
double Value = (double)udRadius.Value; | |
/*tbLeft.Text = (-Value).ToString(); | |
tbRight.Text = (Value).ToString(); | |
tbTop.Text = (Value).ToString(); | |
tbBottom.Text = (-Value).ToString(); | |
*/ | |
PlotGraphic(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment