Необходимо оптимизировать захардкоженный код. В программе работает всё хорошо, но код выглядит не красиво.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace KG
{
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Lines : Point
{
public int Xstart { get; set; }
public int Ystart { get; set; }
public int Xend { get; set; }
public int Yend { get; set; }
}
/// <summary>
/// Логика взаимодействия для LR1.xaml
/// </summary>
public partial class Lr2
{
public int CountHorisontal;
public int CountVertical;
public int IdLabel;
public int SizeX;
public int SizeY;
public Lr2()
{
InitializeComponent();
Background = new SolidColorBrush(Color.FromRgb(30, 30, 30));
}
private void CreateLabel(int i, int j, string z)
{
IdLabel++;
var dynamicLabel = new Label
{
Name = "label" + IdLabel,
Content = z,
FontSize = 10,
FontFamily = new FontFamily("Times New Roman"),
Margin = new Thickness(i, j, 0, 0),
Foreground = new SolidColorBrush(Colors.White)
};
if ((string) dynamicLabel.Content == "0" || (string) dynamicLabel.Content == "-0") return;
Grid.Children.Add(dynamicLabel);
}
private void CreateImage(int moveHorisontal, int moveVertical, int sizeX, int sizeY)
{
SizeX += sizeX;
SizeY += sizeY;
var wb = new WriteableBitmap((int) Img.Width, (int) Img.Height, 200, 200, PixelFormats.Bgra32, null);
const int stride = 200;
const int alpha = 255;
var blue = 255;
var red = 255;
var green = 255;
Int32Rect rect;
byte[] blackColorData = {(byte) blue, (byte) green, (byte) red, alpha};
for (var i = 0; i < 550; i++)
{
var horisontal = new Int32Rect(i, 175, 1, 1);
wb.WritePixels(horisontal, blackColorData, stride, 0);
if (i % 35 != 0) continue;
CreateLabel(235 + i, 175 + 5, "" + i);
CreateLabel(235 - i, 175 + 5, "-" + i);
for (var j = 0; j < 5; j++)
{
var dashTop = new Int32Rect(i, 175 - j, 1, 1);
wb.WritePixels(dashTop, blackColorData, stride, 0);
var dashDown = new Int32Rect(i, 175 + j, 1, 1);
wb.WritePixels(dashDown, blackColorData, stride, 0);
}
}
for (var i = 0; i < 340; i++)
{
var vertical = new Int32Rect(245, i, 1, 1);
wb.WritePixels(vertical, blackColorData, stride, 0);
if (i % 35 != 0) continue;
CreateLabel(245 + 5, 165 - i, "" + i);
CreateLabel(245 + 5, 165 + i, "-" + i);
for (var j = 0; j < 5; j++)
{
var dashLeft = new Int32Rect(245 - j, i, 1, 1);
wb.WritePixels(dashLeft, blackColorData, stride, 0);
var dashRight = new Int32Rect(245 + j, i, 1, 1);
wb.WritePixels(dashRight, blackColorData, stride, 0);
}
}
var a = new Point {X = 300 - SizeX, Y = 100 - SizeY};
var b = new Point {X = 500 + SizeX, Y = 100 - SizeY};
var c = new Point {X = 300 - SizeX, Y = 300 + SizeY};
var d = new Point {X = 500 + SizeX, Y = 300 + SizeY};
var firstLine = new Lines
{
Xstart = a.X + moveHorisontal,
Ystart = a.Y + moveVertical,
Xend = b.X + moveHorisontal,
Yend = b.Y + moveVertical
};
var secondLine = new Lines
{
Xstart = a.X + moveHorisontal,
Ystart = a.Y + moveVertical,
Xend = c.X + moveHorisontal,
Yend = c.Y + moveVertical
};
var thirdLine = new Lines
{
Xstart = c.X + moveHorisontal,
Ystart = c.Y + moveVertical,
Xend = d.X + moveHorisontal,
Yend = d.Y + moveVertical
};
var fourthLine = new Lines
{
Xstart = b.X + moveHorisontal,
Ystart = b.Y + moveVertical,
Xend = d.X + moveHorisontal,
Yend = d.Y + moveVertical
};
// Линия 1
var n = Math.Max(Math.Abs(firstLine.Xend - firstLine.Xstart), Math.Abs(firstLine.Yend - firstLine.Ystart));
var xA = new int[n];
var yA = new int[n];
xA[0] = firstLine.Xstart;
yA[0] = firstLine.Ystart;
for (var i = 1; i < n; i++)
{
xA[i] = xA[i - 1] + (firstLine.Xend - firstLine.Xstart) / n;
yA[i] = yA[i - 1] + (firstLine.Yend - firstLine.Ystart) / n;
}
for (var i = 0; i < n; i++)
{
red = 155 + i;
blue = 100;
green = 100;
byte[] colorData = {(byte) blue, (byte) green, (byte) red, alpha};
rect = new Int32Rect(xA[i], yA[i], 1, 1);
wb.WritePixels(rect, colorData, stride, 0);
}
// Линия 2
n = Math.Max(Math.Abs(secondLine.Xend - secondLine.Xstart), Math.Abs(secondLine.Yend - secondLine.Ystart));
var xB = new int[n];
var yB = new int[n];
xB[0] = secondLine.Xstart;
yB[0] = secondLine.Ystart;
for (var i = 1; i < n; i++)
{
xB[i] = xB[i - 1] + (secondLine.Xend - secondLine.Xstart) / n;
yB[i] = yB[i - 1] + (secondLine.Yend - secondLine.Ystart) / n;
}
for (var i = 0; i < n; i++)
{
red = 155 + i;
blue = 100;
green = 100 + i;
byte[] colorData = {(byte) blue, (byte) green, (byte) red, alpha};
rect = new Int32Rect(xB[i], yB[i], 1, 1);
wb.WritePixels(rect, colorData, stride, 0);
}
// Линия 3
n = Math.Max(Math.Abs(thirdLine.Xend - thirdLine.Xstart), Math.Abs(thirdLine.Yend - thirdLine.Ystart));
var xC = new int[n];
var yC = new int[n];
xC[0] = thirdLine.Xstart;
yC[0] = thirdLine.Ystart;
for (var i = 1; i < n; i++)
{
xC[i] = xC[i - 1] + (thirdLine.Xend - thirdLine.Xstart) / n;
yC[i] = yC[i - 1] + (thirdLine.Yend - thirdLine.Ystart) / n;
}
for (var i = 0; i < n; i++)
{
red = 155;
blue = 100;
green = 100 + i;
byte[] colorData = {(byte) blue, (byte) green, (byte) red, alpha};
rect = new Int32Rect(xC[i], yC[i], 1, 1);
wb.WritePixels(rect, colorData, stride, 0);
}
// Линия 4
n = Math.Max(Math.Abs(fourthLine.Xend - fourthLine.Xstart), Math.Abs(fourthLine.Yend - fourthLine.Ystart));
var xD = new int[n];
var yD = new int[n];
xD[0] = fourthLine.Xstart;
yD[0] = fourthLine.Ystart;
for (var i = 1; i < n; i++)
{
xD[i] = xD[i - 1] + (fourthLine.Xend - fourthLine.Xstart) / n;
yD[i] = yD[i - 1] + (fourthLine.Yend - fourthLine.Ystart) / n;
}
for (var i = 0; i < n; i++)
{
red = 155 + i;
blue = 100;
green = 100 + i;
byte[] colorData = {(byte) blue, (byte) green, (byte) red, alpha};
rect = new Int32Rect(xD[i], yD[i], 1, 1);
wb.WritePixels(rect, colorData, stride, 0);
}
Img.Source = wb;
}
private void CreatePicure(object sender, RoutedEventArgs e)
{
CountHorisontal = -155;
CountVertical = -27;
CreateImage(CountHorisontal, CountVertical, 0, 0);
}
private void Left_Click(object sender, RoutedEventArgs e)
{
CountHorisontal -= 35;
CreateImage(CountHorisontal, CountVertical, 0, 0);
}
private void Right_Click(object sender, RoutedEventArgs e)
{
CountHorisontal += 35;
CreateImage(CountHorisontal, CountVertical, 0, 0);
}
private void Down_Click(object sender, RoutedEventArgs e)
{
CountVertical += 35;
CreateImage(CountHorisontal, CountVertical, 0, 0);
}
private void Top_Click(object sender, RoutedEventArgs e)
{
CountVertical -= 35;
CreateImage(CountHorisontal, CountVertical, 0, 0);
}
private void Increase_Click(object sender, RoutedEventArgs e)
{
CreateImage(CountHorisontal, CountVertical, 10, 10);
}
private void reduce_Click(object sender, RoutedEventArgs e)
{
CreateImage(CountHorisontal, CountVertical, -10, -10);
}
}
}
вполне приличный код. самое главное, что он понятен и его немного. Но если уж так хотите причесать:
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
Краткий смысл: Идут по XDocument в цикле, проверяю условие и если оно истинно, то удаляю узел:
Здравствуйте! При попытке создание отчёта с помощью запроса sql в ReportViewer ничего не отображаетсяКакие могут быть причины? Как можно заполнить...
Здравствуйте, подскажите, пожалуйста, как можно отследить вебсокет который я создаю с помощью websocketsharp?
Всем привет! Я знаю, что очень много информации по поводу практикиОтветов большинство на сайте "создай свой проект" и т