Advertisement
T-D-K

Untitled

Jul 25th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.12 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Security.AccessControl;
  12. using System.Security.Policy;
  13. using System.Text;
  14. using System.Xml;
  15.  
  16. namespace ConsoleApplication10 {
  17.     class Program {
  18.         static void Main(string[] args) {
  19.             PathLine.CustomEndCap = new AdjustableArrowCap(10, 10);
  20.             Connector[] connectors = ReadData();
  21.             foreach(Connector connector in connectors) {
  22.                 if(connector.Preset == 5)
  23.                     continue;
  24.                 Connector actual = Calculate(connector.StartId, connector.EndId, connector.DX, connector.DY, connector.Name, connector);
  25.                 CheckConnectors(connector, actual);
  26.             }
  27.         }
  28.         const int Rotation0 = 0;
  29.         const int Rotation90 = 5400000;
  30.         const int Rotation180 = 10800000;
  31.         const int Rotation270 = 16200000;
  32.         const int Rotation360 = 21600000;
  33.  
  34.         static Connector Calculate(int startId, int endId, int dx, int dy, string name, Connector original) {
  35.             int firstShapeX = 2 + dx;
  36.             int firstShapeY = 2 + dy;
  37.             int secondShapeX = 2 - dx;
  38.             int secondShapeY = 2 - dy;
  39.             int startX = firstShapeX + GetDXByIdx(startId);
  40.             int startY = firstShapeY + GetDYByIdx(startId);
  41.             int finishX = secondShapeX + GetDXByIdx(endId);
  42.             int finishY = secondShapeY + GetDYByIdx(endId);
  43.             PathInfo[,] map = new PathInfo[5, 5];
  44.             for(int i = 0; i < 5; i++) {
  45.                 for(int j = 0; j < 5; j++) {
  46.                     map[i, j] = new PathInfo(i, j);
  47.                     map[i, j].Length = int.MaxValue;
  48.                     map[i, j].Bends = int.MaxValue;
  49.                 }
  50.             }
  51.             map[startX, startY].Length = 0;
  52.             map[startX, startY].Bends = 0;
  53.             map[startX, startY].DX = GetDXByIdx(startId);
  54.             map[startX, startY].DY = GetDYByIdx(startId);
  55.             HashSet<PathInfo> close = new HashSet<PathInfo>();
  56.             MySortedList open = new MySortedList();
  57.             close.Add(map[firstShapeX, firstShapeY]);
  58.             close.Add(map[secondShapeX, secondShapeY]);
  59.             open.Add(map[startX, startY]);
  60.             int[,] offsets = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };
  61.             while(open.Count > 0) {
  62.                 PathInfo current = open.GetMinAndRemove();
  63.                 close.Add(current);
  64.                 for(int i = 0; i < 4; i++) {
  65.                     int newX = current.X + offsets[i, 0];
  66.                     int newY = current.Y + offsets[i, 1];
  67.                     if(!IsPoint(newX, newY))
  68.                         continue;
  69.                     PathInfo other = map[newX, newY];
  70.                     if(close.Contains(other))
  71.                         continue;
  72.                     if(!open.Contains(other))
  73.                         open.Add(other);
  74.                     int newLength = current.Length + 1;
  75.                     int newBends = offsets[i, 0] == current.DX && offsets[i, 1] == current.DY ? current.Bends : current.Bends + 1;
  76.                     if(other.Length > newLength || (other.Length == newLength && other.Bends > newBends)) {
  77.                         other.Length = newLength;
  78.                         other.Bends = newBends;
  79.                         other.DX = offsets[i, 0];
  80.                         other.DY = offsets[i, 1];
  81.                     }
  82.                 }
  83.             }
  84.             List<Point> path = GetPath(map, startX, startY, finishX, finishY, firstShapeX, firstShapeY, secondShapeX, secondShapeY);
  85.             path = OptimizePath(path);
  86.             DrawPictureByMapAndSave(name, finishX, finishY, startX, startY, path, firstShapeX, firstShapeY, secondShapeX, secondShapeY);
  87.             Connector connector = new Connector();
  88.             connector.DX = dx;
  89.             connector.DY = dy;
  90.             connector.StartId = startId;
  91.             connector.EndId = endId;
  92.             int preset = map[finishX, finishY].Bends + 1;
  93.             if(map[finishX, finishY].DX != -GetDXByIdx(endId) || map[finishX, finishY].DY != -GetDYByIdx(endId))
  94.                 preset++;
  95.             if(preset == 1)
  96.                 preset = 3;
  97.             connector.Preset = preset;
  98.             connector.Rotation = GetRotation(connector);
  99.             switch(connector.Preset) {
  100.                 case 2:
  101.                     SetupProperties2(path, connector, (int) (connector.Rotation / 60000));
  102.                     break;
  103.                 case 3:
  104.                     FillAdjust3(path, connector);
  105.                     SetupProperties3(path, connector, (int) (connector.Rotation / 60000));
  106.                     break;
  107.                 case 4:
  108.                     FilltAdjust4(path, connector);
  109.                     SetupProperties4(path, connector, (int) (connector.Rotation / 60000));
  110.                     break;
  111.                 case 5:
  112.                     FillAdjust5(connector);
  113.                     break;
  114.             }
  115.  
  116.             int tempLeft = (int)((firstShapeX + 0.5 + GetDXByIdx(startId) / 2f) * ShapeSize);
  117.             int tempTop = (int)((firstShapeY + 0.5 + GetDYByIdx(startId) / 2f) * ShapeSize);
  118.             int tempRight = (int)((secondShapeX + 0.5 + GetDXByIdx(endId) / 2f) * ShapeSize);
  119.             int tempBottom = (int)((secondShapeY + 0.5 + GetDYByIdx(endId) / 2f) * ShapeSize);
  120.             int left = Math.Min(tempLeft, tempRight);
  121.             int top = Math.Min(tempTop, tempBottom);
  122.             int width = Math.Abs(tempRight - tempLeft);
  123.             int height = Math.Abs(tempBottom - tempTop);
  124.             if(original.Rotation == Rotation90 || original.Rotation == Rotation270) {
  125.                 left = left + width / 2 - height / 2;
  126.                 top = top + height / 2 - width / 2;
  127.                 int temp = width;
  128.                 width = height;
  129.                 height = temp;
  130.             }
  131.             DrawPictureByConnector(name, left, top, width, height, connector, firstShapeX, firstShapeY, secondShapeX, secondShapeY);
  132.             if(connector.FlipH)
  133.                 connector.Rotation = Rotation360 - connector.Rotation;
  134.             if(connector.FlipV)
  135.                 connector.Rotation = Rotation360 - connector.Rotation;
  136.             connector.Rotation %= Rotation360;
  137.             return connector;
  138.         }
  139.  
  140.         static long GetRotation(Connector connector) {
  141.             if (connector.DX == 1) {
  142.                 switch (connector.DY) {
  143.                     case -1:
  144.                         return (3 - connector.StartId) * 90 * 60000;
  145.                     case 1:
  146.                         return (360 - (3 - connector.StartId) * 90) % 360 * 60000;
  147.                 }
  148.                 throw new Exception();
  149.             }
  150.             switch (connector.StartId) {
  151.                 case 0:
  152.                 case 2:
  153.                     return 90 * 60000;
  154.                 case 3:
  155.                     return 0 * 60000;
  156.                 case 1:
  157.                     return 180 * 60000;
  158.             }
  159.             throw new Exception();
  160.         }
  161.  
  162.         #region FillDefaulAdjust
  163.         static void FillAdjust5(Connector connector) {
  164.  
  165.         }
  166.  
  167.         static void FilltAdjust4(List<Point> path, Connector connector) {
  168.             int[] signedLength = new int[path.Count - 1];
  169.             int abssum = 0;
  170.             int[] how = new int[4];
  171.             for(int i = 0; i < signedLength.Length; i++) {
  172.                 signedLength[i] = path[i + 1].X - path[i].X + path[i + 1].Y - path[i].Y;
  173.                 abssum += signedLength[i];
  174.                 how[Math.Abs(signedLength[i])]++;
  175.             }
  176.             abssum = Math.Abs(abssum);
  177.             if(how[1] == 4) {
  178.                 if(signedLength[1] == signedLength[3]) {
  179.                     connector.AdjustValues.Add(-36000);
  180.                     connector.AdjustValues.Add(75000);
  181.                 }
  182.                 else if(signedLength[0] == signedLength[2]) {
  183.                     connector.AdjustValues.Add(25000);
  184.                     connector.AdjustValues.Add(136000);
  185.                 }
  186.                 else
  187.                     throw new Exception();
  188.             }
  189.             else if(how[3] == 2) {
  190.                 connector.AdjustValues.Add(-9000);
  191.                 connector.AdjustValues.Add(109000);
  192.             }
  193.             else if(how[3] == 1) {
  194.                 if(Math.Abs(signedLength[1]) == 3) {
  195.                     if(abssum == 2) {
  196.                         connector.AdjustValues.Add(-36000);
  197.                         connector.AdjustValues.Add(109000);
  198.                     }
  199.                     else if(abssum == 4 || abssum == 0) {
  200.                         connector.AdjustValues.Add(25000);
  201.                         connector.AdjustValues.Add(109000);
  202.                     }
  203.                     else
  204.                         throw new Exception();
  205.                 }
  206.                 else if(Math.Abs(signedLength[2]) == 3) {
  207.                     if(abssum == 2) {
  208.                         connector.AdjustValues.Add(-9000);
  209.                         connector.AdjustValues.Add(136000);
  210.                     }
  211.                     else if(abssum == 4 || abssum == 0) {
  212.                         connector.AdjustValues.Add(-9000);
  213.                         connector.AdjustValues.Add(75000);
  214.                     }
  215.                     else
  216.                         throw new Exception();
  217.                 }
  218.             }
  219.             else
  220.                 throw new Exception();
  221.         }
  222.  
  223.         static void FillAdjust3(List<Point> path, Connector connector) {
  224.             switch(path.Count) {
  225.                 case 2:
  226.                     connector.AdjustValues.Add(50000);
  227.                     break;
  228.                 case 4:
  229.                     int[] signedLength = new int[3];
  230.                     for(int i = 0; i < 3; i++) {
  231.                         signedLength[i] = path[i + 1].X - path[i].X + path[i + 1].Y - path[i].Y;
  232.                     }
  233.                     if(signedLength[0] == signedLength[2]) {
  234.                         connector.AdjustValues.Add(50000);
  235.                     }
  236.                     else if(signedLength[0] == -signedLength[2]) {
  237.                         connector.AdjustValues.Add(1800000); //тут надо делать ширину коннектора равной 1 поинт == 20 твипсов
  238.                     }
  239.                     else if(Math.Abs(signedLength[0]) == 1) {
  240.                         connector.AdjustValues.Add(-12000); //эти adjustValues и т.д. не подходят в общем случае, надо добиваться отступа от шейпов в 360 твипсов
  241.                     }
  242.                     else if(Math.Abs(signedLength[0]) == 3) {
  243.                         connector.AdjustValues.Add(112000);
  244.                     }
  245.                     else
  246.                         throw new Exception();
  247.                     break;
  248.                 default:
  249.                     throw new Exception();
  250.             }
  251.         }
  252.         #endregion
  253.         #region SetupProperties
  254.  
  255.         static void SetupProperties2(List<Point> path, Connector connector, int rotation) {
  256.             int[] expectedAngles = GetAngles(path);
  257.             int[] actualAngles = GetActualAngles2();
  258.             for(int i = 0; i < actualAngles.Length; i++) {
  259.                 actualAngles[i] += rotation;
  260.                 actualAngles[i] %= 360;
  261.             }
  262.             connector.Rotation = rotation * 60000;
  263.             for(int i = 0; i < expectedAngles.Length; i++) {
  264.                 FixAngleByFlip(expectedAngles[i], actualAngles[i], connector, actualAngles);
  265.             }
  266.         }
  267.  
  268.         static void SetupProperties3(List<Point> path, Connector connector, int rotation) {
  269.             int[] actualAngles = GetActualAngles3(path);
  270.             int[] expectedAngles = GetAngles(path);
  271.             if(actualAngles.Length != expectedAngles.Length)
  272.                 throw new Exception();
  273.             for(int i = 0; i < actualAngles.Length; i++) {
  274.                 actualAngles[i] += rotation;
  275.                 actualAngles[i] %= 360;
  276.             }
  277.             connector.Rotation = rotation * 60000;
  278.             for(int i = 0; i < actualAngles.Length; i++) {
  279.                 FixAngleByFlip(expectedAngles[i], actualAngles[i], connector, actualAngles);
  280.             }
  281.         }
  282.         static void SetupProperties4(List<Point> path, Connector connector, int rotation) {
  283.             int[] actualAngles = GetActualAngles4(connector);
  284.             int[] expectedAngles = GetAngles(path);
  285.             if(actualAngles.Length != expectedAngles.Length)
  286.                 throw new Exception();
  287.             if(rotation == 270) {
  288.                 rotation = 90;
  289.             }
  290.             for(int i = 0; i < actualAngles.Length; i++) {
  291.                 actualAngles[i] += rotation;
  292.                 actualAngles[i] %= 360;
  293.             }
  294.             connector.Rotation = rotation * 60000;
  295.             for(int i = 0; i < actualAngles.Length; i++) {
  296.                 FixAngleByFlip(expectedAngles[i], actualAngles[i], connector, actualAngles);
  297.             }
  298.         }
  299.         #endregion
  300.         static void FixAngleByFlip(int expected, int actual, Connector connector, int[] actualAngles) {
  301.             if(expected == actual)
  302.                 return;
  303.             if(expected == 0 || expected == 180) {
  304.                 MakeFlipH(connector, actualAngles);
  305.             }
  306.             else {
  307.                 MakeFlipV(connector, actualAngles);
  308.             }
  309.         }
  310.         static int GetRotationByIdx(int idx) {
  311.             switch(idx) {
  312.                 case 0:
  313.                     return 270;
  314.                 case 1:
  315.                     return 180;
  316.                 case 2:
  317.                     return 90;
  318.                 case 3:
  319.                     return 0;
  320.                 default:
  321.                     throw new Exception();
  322.             }
  323.         }
  324.         #region MakeFlip
  325.         static void MakeFlipV(Connector connector, int[] actualAngles) {
  326.             connector.FlipV = true;
  327.             for(int i = 0; i < actualAngles.Length; i++) {
  328.                 actualAngles[i] = FlipV(actualAngles[i]);
  329.             }
  330.         }
  331.  
  332.         static void MakeFlipH(Connector connector, int[] actualAngles) {
  333.             connector.FlipH = true;
  334.             for(int i = 0; i < actualAngles.Length; i++) {
  335.                 actualAngles[i] = FlipH(actualAngles[i]);
  336.             }
  337.         }
  338.  
  339.         static int FlipV(int angle) {
  340.             if(angle == 90 || angle == 270)
  341.                 return (angle + 180) % 360;
  342.             return angle;
  343.         }
  344.         static int FlipH(int angle) {
  345.             if(angle == 0 || angle == 180)
  346.                 return (angle + 180) % 360;
  347.             return angle;
  348.         }
  349.         #endregion
  350.         #region GetAngles
  351.         static int[] GetActualAngles2() {
  352.             return new[] { 0, 90 };
  353.         }
  354.         static int[] GetActualAngles3(List<Point> path) {
  355.             switch(path.Count) {
  356.                 case 2:
  357.                     return new[] { 0 };
  358.                 case 4:
  359.                     int[] signedLength = new int[3];
  360.                     for(int i = 0; i < 3; i++) {
  361.                         signedLength[i] = path[i + 1].X - path[i].X + path[i + 1].Y - path[i].Y;
  362.                     }
  363.                     if(signedLength[0] == signedLength[2]) {
  364.                         return new[] { 0, 90, 0 };
  365.                     }
  366.                     if(signedLength[0] == -signedLength[2]) {
  367.                         return new[] { 0, 90, 180 };
  368.                     }
  369.                     if(Math.Abs(signedLength[0]) == 1) {
  370.                         return new[] { 180, 90, 0 };
  371.                     }
  372.                     if(Math.Abs(signedLength[0]) == 3) {
  373.                         return new[] { 0, 90, 180 };
  374.                     }
  375.                     throw new Exception();
  376.                 default:
  377.                     throw new Exception();
  378.             }
  379.         }
  380.  
  381.         static int[] GetActualAngles4(Connector connector) {
  382.             int[] result = new int[4];
  383.             result[0] = connector.AdjustValues[0] == 25000 ? 0 : 180;
  384.             result[1] = 90;
  385.             result[2] = 0;
  386.             result[3] = connector.AdjustValues[1] == 75000 ? 90 : 270;
  387.             return result;
  388.         }
  389.  
  390.         static int[] GetAngles(List<Point> path) {
  391.             int[] result = new int[path.Count - 1];
  392.             for(int i = 0; i < path.Count - 1; i++) {
  393.                 int dx = path[i + 1].X - path[i].X;
  394.                 int dy = path[i + 1].Y - path[i].Y;
  395.                 result[i] = dy == 0 ? dx > 0 ? 0 : 180 : dy > 0 ? 90 : 270;
  396.             }
  397.             return result;
  398.         }
  399.  
  400.         #endregion
  401.         #region Path Stuff
  402.         static List<Point> OptimizePath(List<Point> path) {
  403.             if(path.Count == 1)
  404.                 return path;
  405.             List<Point> result = new List<Point>();
  406.             Point currentStart = path[0];
  407.             result.Add(currentStart);
  408.             Point currentFinish = path[1];
  409.             int dx = currentFinish.X - currentStart.X;
  410.             int dy = currentFinish.Y - currentStart.Y;
  411.             for(int i = 2; i < path.Count; i++) {
  412.                 if(dx == path[i].X - currentFinish.X && dy == path[i].Y - currentFinish.Y) {
  413.                     currentFinish = path[i];
  414.                 }
  415.                 else {
  416.                     result.Add(currentFinish);
  417.                     currentStart = currentFinish;
  418.                     currentFinish = path[i];
  419.                     dx = currentFinish.X - currentStart.X;
  420.                     dy = currentFinish.Y - currentStart.Y;
  421.                 }
  422.             }
  423.             result.Add(currentFinish);
  424.             return result;
  425.         }
  426.  
  427.         static List<Point> GetPath(PathInfo[,] map, int startX, int startY, int finishX, int finishY, int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY) {
  428.             List<Point> result = new List<Point>();
  429.             result.Add(new Point(secondShapeX, secondShapeY));
  430.             int x = finishX;
  431.             int y = finishY;
  432.             result.Add(new Point(x, y));
  433.             while(!(x == startX && y == startY)) {
  434.                 int nextX = x - map[x, y].DX;
  435.                 int nextY = y - map[x, y].DY;
  436.                 x = nextX;
  437.                 y = nextY;
  438.                 result.Add(new Point(x, y));
  439.             }
  440.             result.Add(new Point(firstShapeX, firstShapeY));
  441.             result.Reverse();
  442.             return result;
  443.         }
  444.         #endregion
  445.  
  446.         const int ShapeSize = 100;
  447.         static readonly Pen PathLine = new Pen(Color.Blue, 2);
  448.         static readonly Brush ShapesBrush = new SolidBrush(Color.Green);
  449.         static readonly Font ShapesFont = new Font("Courier", 16);
  450.         static readonly SolidBrush FontBrush = new SolidBrush(Color.Red);
  451.         static readonly Pen CellsPen = new Pen(Color.White, 2);
  452.  
  453.         static void DrawPictureByConnector(string name, long left, long top, long width, long height, Connector connector, int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY) {
  454.             Bitmap bmp = new Bitmap(ShapeSize * 5 + 1, ShapeSize * 5 + 1);
  455.             Graphics gr = Graphics.FromImage(bmp);
  456.  
  457.             DrawShapes(firstShapeX, firstShapeY, secondShapeX, secondShapeY, gr);
  458.             DrawCells(gr);
  459.  
  460.             float angle = connector.Rotation / 60000f;
  461.             Matrix matrix = new Matrix();
  462.             long offsetX = 2 * left + width;
  463.             long offsetY = 2 * top + height;
  464.             if(connector.FlipV) {
  465.                 matrix.Multiply(new Matrix(1, 0, 0, -1, 0, 0), MatrixOrder.Append);
  466.                 matrix.Translate(0, offsetY, MatrixOrder.Append);
  467.             }
  468.             if(connector.FlipH) {
  469.                 matrix.Multiply(new Matrix(-1, 0, 0, 1, 0, 0), MatrixOrder.Append);
  470.                 matrix.Translate(offsetX, 0, MatrixOrder.Append);
  471.             }
  472.  
  473.             PointF center = new PointF(left + width / 2f, top + height / 2f);
  474.             matrix.RotateAt(angle, center);
  475.             gr.Transform = matrix;
  476.             switch(connector.Preset) {
  477.                 case 2:
  478.                     DrawPreset2(gr, left, top, width, height, connector);
  479.                     break;
  480.                 case 3:
  481.                     DrawPreset3(gr, left, top, width, height, connector);
  482.                     break;
  483.                 case 4:
  484.                     DrawPreset4(gr, left, top, width, height, connector);
  485.                     break;
  486.                 case 5:
  487.                     DrawPreset5();
  488.                     break;
  489.             }
  490.             gr.Transform = new Matrix();
  491.  
  492.             bmp.Save(@"ByPath\\" + name + ".bmp");
  493.         }
  494.         #region DrawPreset
  495.         static void DrawPreset5() {
  496.  
  497.         }
  498.         static void DrawPreset4(Graphics graphics, long left, long top, long width, long height, Connector connector) {
  499.             long adjustValue1 = connector.AdjustValues[0];
  500.             long adjustValue2 = connector.AdjustValues[1];
  501.             float x1 = width * adjustValue1 / 100000f;
  502.             float y2 = height * adjustValue2 / 100000f;
  503.             graphics.DrawLine(PathLine, left, top, left + x1, top);
  504.             graphics.DrawLine(PathLine, left + x1, top, left + x1, top + y2);
  505.             graphics.DrawLine(PathLine, left + x1, top + y2, left + width, top + y2);
  506.             graphics.DrawLine(PathLine, left + width, top + y2, left + width, top + height);
  507.         }
  508.  
  509.         static void DrawPreset3(Graphics graphics, long left, long top, long width, long height, Connector connector) {
  510.             long adjustValue = connector.AdjustValues[0];
  511.             const int d = ShapeSize / 2;
  512.             switch(adjustValue) {
  513.                 case -12000:
  514.                     adjustValue = -d * 100000 / width;
  515.                     break;
  516.                 case 50000:
  517.                     break;
  518.                 case 112000:
  519.                     adjustValue = (d + width) * 100000 / width;
  520.                     break;
  521.                 case 1800000:
  522.                     width = 1;
  523.                     adjustValue = d * 100000 / width;
  524.                     break;
  525.             }
  526.             float x1 = width * adjustValue / 100000f;
  527.             graphics.DrawLine(PathLine, left, top, left + x1, top);
  528.             graphics.DrawLine(PathLine, left + x1, top, left + x1, top + height);
  529.             graphics.DrawLine(PathLine, left + x1, top + height, left + width, top + height);
  530.         }
  531.  
  532.         static void DrawPreset2(Graphics graphics, long left, long top, long width, long height, Connector connector) {
  533.             graphics.DrawLine(PathLine, left, top, left + width, top);
  534.             graphics.DrawLine(PathLine, left + width, top, left + width, top + height);
  535.         }
  536.         #endregion
  537.         static void DrawPictureByMapAndSave(string name, int finishX, int finishY, int startX, int startY, List<Point> path, int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY) {
  538.             Bitmap bmp = new Bitmap(ShapeSize * 5 + 1, ShapeSize * 5 + 1);
  539.             Graphics gr = Graphics.FromImage(bmp);
  540.  
  541.             List<Point> fullPath = new List<Point>();
  542.             //fullPath.Add(new Point(firstShapeX, firstShapeY));
  543.             fullPath.AddRange(path);
  544.             //fullPath.Add(new Point(secondShapeX, secondShapeY));
  545.  
  546.             for(int index = 0; index < fullPath.Count - 1; index++) {
  547.                 Point point = fullPath[index];
  548.                 Point nextPoint = fullPath[index + 1];
  549.                 int x = point.X;
  550.                 int y = point.Y;
  551.                 int nextX = nextPoint.X;
  552.                 int nextY = nextPoint.Y;
  553.                 gr.DrawLine(PathLine, x * ShapeSize + ShapeSize / 2, y * ShapeSize + ShapeSize / 2, nextX * ShapeSize + ShapeSize / 2, nextY * ShapeSize + ShapeSize / 2);
  554.             }
  555.  
  556.             DrawShapes(firstShapeX, firstShapeY, secondShapeX, secondShapeY, gr);
  557.  
  558.             DrawCells(gr);
  559.  
  560.             bmp.Save(@"Pictures\\" + name + ".bmp");
  561.         }
  562.         static void DrawShapes(int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY, Graphics gr) {
  563.             SizeF textSize = gr.MeasureString("1", ShapesFont);
  564.             gr.FillRectangle(ShapesBrush, firstShapeX * ShapeSize, firstShapeY * ShapeSize, ShapeSize, ShapeSize);
  565.             gr.FillRectangle(ShapesBrush, secondShapeX * ShapeSize, secondShapeY * ShapeSize, ShapeSize, ShapeSize);
  566.  
  567.             gr.DrawString("1", ShapesFont, FontBrush, firstShapeX * ShapeSize + (ShapeSize - textSize.Width) / 2, firstShapeY * ShapeSize + (ShapeSize - textSize.Height) / 2);
  568.             gr.DrawString("2", ShapesFont, FontBrush, secondShapeX * ShapeSize + (ShapeSize - textSize.Width) / 2, secondShapeY * ShapeSize + (ShapeSize - textSize.Height) / 2);
  569.         }
  570.         static void DrawCells(Graphics gr) {
  571.             CellsPen.DashStyle = DashStyle.Dash;
  572.             for(int i = 0; i < 6; i++) {
  573.                 gr.DrawLine(CellsPen, 0, ShapeSize * i, ShapeSize * 5 + 1, ShapeSize * i);
  574.                 gr.DrawLine(CellsPen, ShapeSize * i, 0, ShapeSize * i, ShapeSize * 5 + 1);
  575.             }
  576.         }
  577.  
  578.         static bool IsPoint(int x, int y) {
  579.             return x >= 0 && y >= 0 && x < 5 && y < 5;
  580.         }
  581.  
  582.         public static int GetDXByIdx(int idx) {
  583.             switch(idx) {
  584.                 case 0:
  585.                     return 0;
  586.                 case 1:
  587.                     return -1;
  588.                 case 2:
  589.                     return 0;
  590.                 case 3:
  591.                     return 1;
  592.             }
  593.             throw new Exception();
  594.         }
  595.  
  596.         public static int GetDYByIdx(int idx) {
  597.             switch(idx) {
  598.                 case 0:
  599.                     return -1;
  600.                 case 1:
  601.                     return 0;
  602.                 case 2:
  603.                     return 1;
  604.                 case 3:
  605.                     return 0;
  606.             }
  607.             throw new Exception();
  608.         }
  609.  
  610.         static void CheckConnectors(Connector expected, Connector actual) {
  611.             //if(expected.FlipH != actual.FlipH)
  612.             //    Log("FlipH: Expected: {0} != Actual: {1}, Name:{2}", expected.FlipH, actual.FlipH, expected.Name);
  613.             //if(expected.FlipV != actual.FlipV)
  614.             //    Log("FlipV: Expected: {0} != Actual: {1}, Name:{2}", expected.FlipV, actual.FlipV, expected.Name);
  615.             if(expected.Rotation != actual.Rotation)
  616.                 Log("Rotation: Expected: {0} != Actual: {1}, Name:{2}", expected.Rotation, actual.Rotation, expected.Name);
  617.             //if(expected.Preset != actual.Preset)
  618.             //    Log("Preset: Expected: {0} != Actual: {1}, Name:{2}", expected.Preset, actual.Preset, expected.Name);
  619.             //if(expected.AdjustValues.Count != actual.AdjustValues.Count)
  620.             //    Log("AdjustValues.Count: Expected: {0} != Actual: {1}, Name:{2}", expected.AdjustValues.Count, actual.AdjustValues.Count, expected.Name);
  621.             //else {
  622.             //    for(int i = 0; i < expected.AdjustValues.Count; i++) {
  623.             //        if(expected.AdjustValues[i] != actual.AdjustValues[i])
  624.             //            Log("AdjustValue {0}: {1} != {2}", i, expected.AdjustValues[i], actual.AdjustValues[i]);
  625.             //    }
  626.             //}
  627.         }
  628.  
  629.         static void Log(string message, params object[] par) {
  630.             //throw new Exception(message);
  631.             Console.WriteLine(message, par);
  632.         }
  633.  
  634.         static Connector[] ReadData() {
  635.             const string fileName = "1.xml";
  636.             XmlDocument document = new XmlDocument();
  637.             document.Load(fileName);
  638.             XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
  639.             nsmgr.AddNamespace("xdr", "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing");
  640.             nsmgr.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
  641.             XmlNodeList cxnSpPrs = document.SelectNodes("*//xdr:nvCxnSpPr", nsmgr);
  642.             Connector[] connectors = new Connector[96];
  643.             for(int i = 0; i < cxnSpPrs.Count; i++) {
  644.                 Connector connector = new Connector();
  645.                 connectors[i] = connector;
  646.                 XmlNode cxnSpPr = cxnSpPrs[i];
  647.                 connector.Name = cxnSpPr.ChildNodes[0].Attributes["name"].Value;
  648.                 connector.StartId = int.Parse(cxnSpPr.ChildNodes[1].ChildNodes[0].Attributes["idx"].Value);
  649.                 connector.EndId = int.Parse(cxnSpPr.ChildNodes[1].ChildNodes[1].Attributes["idx"].Value);
  650.                 XmlNode spPr = cxnSpPr.NextSibling;
  651.                 XmlNode xfrm = spPr.ChildNodes[0];
  652.                 XmlNode prstGeom = spPr.ChildNodes[1];
  653.                 XmlAttribute rotationAttribute = xfrm.Attributes["rot"];
  654.                 if(rotationAttribute != null)
  655.                     connector.Rotation = int.Parse(rotationAttribute.Value);
  656.                 if(xfrm.Attributes["flipH"] != null)
  657.                     connector.FlipH = true;
  658.                 if(xfrm.Attributes["flipV"] != null)
  659.                     connector.FlipV = true;
  660.                 connector.Preset = prstGeom.Attributes["prst"].Value.Last() - '0';
  661.                 Dictionary<string, long> adjustas = new Dictionary<string, long>();
  662.                 foreach(XmlNode gdNode in prstGeom.ChildNodes[0].ChildNodes) {
  663.                     adjustas.Add(gdNode.Attributes["name"].Value, long.Parse(gdNode.Attributes["fmla"].Value.Replace("val ", String.Empty)));
  664.                 }
  665.                 long value;
  666.                 switch(connector.Preset) {
  667.                     case 3:
  668.                         if(!adjustas.TryGetValue("adj1", out value))
  669.                             value = 50000;
  670.                         connector.AdjustValues.Add(value);
  671.                         break;
  672.                     case 4:
  673.                         if(!adjustas.TryGetValue("adj1", out value))
  674.                             value = 50000;
  675.                         connector.AdjustValues.Add(value);
  676.                         if(!adjustas.TryGetValue("adj2", out value))
  677.                             value = 50000;
  678.                         connector.AdjustValues.Add(value);
  679.                         break;
  680.                     case 5:
  681.                         if(!adjustas.TryGetValue("adj1", out value))
  682.                             value = 50000;
  683.                         connector.AdjustValues.Add(value);
  684.                         if(!adjustas.TryGetValue("adj2", out value))
  685.                             value = 50000;
  686.                         connector.AdjustValues.Add(value);
  687.                         if(!adjustas.TryGetValue("adj3", out value))
  688.                             value = 50000;
  689.                         connector.AdjustValues.Add(value);
  690.                         break;
  691.                 }
  692.             }
  693.             XmlNodeList xfrms = document.SelectNodes("*//a:off", nsmgr);
  694.             for(int i = 0; i < 96; i++) {
  695.                 XmlNode fromNode = xfrms[i * 3];
  696.                 XmlNode toNode = xfrms[i * 3 + 1];
  697.                 long fromX = long.Parse(fromNode.Attributes["x"].Value);
  698.                 long toX = long.Parse(toNode.Attributes["x"].Value);
  699.                 long fromY = long.Parse(fromNode.Attributes["y"].Value);
  700.                 long toY = long.Parse(toNode.Attributes["y"].Value);
  701.                 connectors[i].DX = fromX.CompareTo(toX);
  702.                 connectors[i].DY = fromY.CompareTo(toY);
  703.             }
  704.             return connectors;
  705.         }
  706.     }
  707.  
  708.     class Connector {
  709.         public int StartId { get; set; }
  710.         public int EndId { get; set; }
  711.         public string Name { get; set; }
  712.         public long Rotation { get; set; }
  713.         public bool FlipH { get; set; }
  714.         public bool FlipV { get; set; }
  715.         public int Preset { get; set; }
  716.         public List<long> AdjustValues { get; set; }
  717.         public int DX { get; set; }
  718.         public int DY { get; set; }
  719.         public Connector() {
  720.             AdjustValues = new List<long>();
  721.         }
  722.     }
  723.  
  724.     class PathInfo : IEquatable<PathInfo>, IComparable<PathInfo> {
  725.         public int X { get; private set; }
  726.         public int Y { get; private set; }
  727.         public int Length { get; set; }
  728.         public int Bends { get; set; }
  729.         public int DX { get; set; }
  730.         public int DY { get; set; }
  731.         public PathInfo(int x, int y) {
  732.             X = x;
  733.             Y = y;
  734.         }
  735.         #region Overrides of ValueType
  736.         public int CompareTo(PathInfo other) {
  737.             int result = Length.CompareTo(other.Length);
  738.             if(result == 0)
  739.                 result = Bends.CompareTo(other.Bends);
  740.             return result;
  741.         }
  742.         public override bool Equals(object obj) {
  743.             if(ReferenceEquals(null, obj))
  744.                 return false;
  745.             return obj is PathInfo && Equals((PathInfo)obj);
  746.         }
  747.         #region Equality members
  748.         public bool Equals(PathInfo other) {
  749.             return X == other.X && Y == other.Y;
  750.         }
  751.         public override int GetHashCode() {
  752.             return (X << 4) + Y;
  753.         }
  754.         #endregion
  755.         #endregion
  756.     }
  757.  
  758.     class MySortedList {
  759.         readonly HashSet<PathInfo> innerList;
  760.         public int Count { get { return innerList.Count; } }
  761.         public MySortedList() {
  762.             innerList = new HashSet<PathInfo>();
  763.         }
  764.         public void Add(PathInfo value) {
  765.             innerList.Add(value);
  766.         }
  767.  
  768.         public bool Contains(PathInfo value) {
  769.             return innerList.Contains(value);
  770.         }
  771.  
  772.         public PathInfo GetMinAndRemove() {
  773.             if(Count == 0)
  774.                 throw new Exception();
  775.             bool first = true;
  776.             PathInfo min = null;
  777.             foreach(PathInfo pathInfo in innerList) {
  778.                 if(first) {
  779.                     min = pathInfo;
  780.                     first = false;
  781.                 }
  782.                 else {
  783.                     if(pathInfo.CompareTo(min) == -1)
  784.                         min = pathInfo;
  785.                 }
  786.             }
  787.             innerList.Remove(min);
  788.             return min;
  789.         }
  790.     }
  791. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement