public void CircleD_Parse_CanRoundTrip() { var circle1 = CircleD.Parse("123.4 456.7 100"); var circle2 = CircleD.Parse(circle1.ToString()); TheResultingValue(circle1 == circle2).ShouldBe(true); }
public void CircleD_SerializesToJson_WhenNullable() { var circle = new CircleD(1.2, 2.3, 3.4); var json = JsonConvert.SerializeObject((CircleD?)circle); TheResultingString(json).ShouldBe(@"{""x"":1.2,""y"":2.3,""radius"":3.4}"); }
public void CircleD_TryParse_FailsForInvalidStrings() { var result = default(CircleD); var succeeded = CircleD.TryParse("foo", out result); TheResultingValue(succeeded).ShouldBe(false); }
public void CircleD_IsConstructedProperly() { var result = new CircleD(123.45, 456.78, 100.10); TheResultingValue(result) .ShouldHavePosition(123.45, 456.78) .ShouldHaveRadius(100.10); }
public void CircleD_SerializesToJson() { var circle = new CircleD(1.2, 2.3, 3.4); var json = JsonConvert.SerializeObject(circle, UltravioletJsonSerializerSettings.Instance); TheResultingString(json).ShouldBe(@"{""x"":1.2,""y"":2.3,""radius"":3.4}"); }
public void CircleD_EqualsObject() { var circle1 = new CircleD(123.45, 456.78, 100.10); var circle2 = new CircleD(123.45, 456.78, 100.10); TheResultingValue(circle1.Equals((Object)circle2)).ShouldBe(true); TheResultingValue(circle1.Equals("This is a test")).ShouldBe(false); }
/// <summary> /// 判断点P是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="P">点P</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C, PointD P) { //判断点是否在圆内: //计算圆心到该点的距离,如果小于等于半径则该点在圆内。 Double D = PointAlgorithm.Distance(P, C.Center); return((D < C.Radius) || DoubleAlgorithm.Equals(D, C.Radius)); }
public void CircleD_Parse_SucceedsForValidStrings() { var str = "123.45 456.78 100.10"; var result = CircleD.Parse(str); TheResultingValue(result) .ShouldHavePosition(123.45, 456.78) .ShouldHaveRadius(100.10); }
public void CircleD_EqualsCircleD() { var circle1 = new CircleD(123.45, 456.78, 100.10); var circle2 = new CircleD(123.45, 456.78, 100.10); var circle3 = new CircleD(123.45, 555.55, 100.10); var circle4 = new CircleD(222.22, 456.78, 100.10); TheResultingValue(circle1.Equals(circle2)).ShouldBe(true); TheResultingValue(circle1.Equals(circle3)).ShouldBe(false); TheResultingValue(circle1.Equals(circle4)).ShouldBe(false); }
/// <summary> /// 获取线段L与圆C的交点集合 /// </summary> /// <param name="L">线段L</param> /// <param name="C">圆C</param> /// <returns>返回交点集合.</returns> public static PointD[] Intersection(LineD L, CircleD C) { List <PointD> result = new List <PointD>(); Int32? has = HasIntersection(L, C); if (has == 0 || has == null) { return(result.ToArray()); } //Points P (x,y) on a line defined by two points P1 (x1,y1,z1) and P2 (x2,y2,z2) is described by //P = P1 + u (P2 - P1) //or in each coordinate //x = x1 + u (x2 - x1) //y = y1 + u (y2 - y1) //z = z1 + u (z2 - z1) //A sphere centered at P3 (x3,y3,z3) with radius r is described by //(x - x3)2 + (y - y3)2 + (z - z3)2 = r2 //Substituting the equation of the line into the sphere gives a quadratic equation of the form //a u2 + b u + c = 0 //where: //a = (x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2 //b = 2[ (x2 - x1) (x1 - x3) + (y2 - y1) (y1 - y3) + (z2 - z1) (z1 - z3) ] //c = x32 + y32 + z32 + x12 + y12 + z12 - 2[x3 x1 + y3 y1 + z3 z1] - r2 //The solutions to this quadratic are described by PointD PD = PointAlgorithm.Substract(L.Starting, L.End); Double a = PD.X * PD.X + PD.Y * PD.Y; Double b = 2 * ((L.End.X - L.Starting.X) * (L.Starting.X - C.Center.X) + (L.End.Y - L.Starting.Y) * (L.Starting.Y - C.Center.Y)); Double c = C.Center.X * C.Center.X + C.Center.Y * C.Center.Y + L.Starting.X * L.Starting.X + L.Starting.Y * L.Starting.Y - 2 * (C.Center.X * L.Starting.X + C.Center.Y * L.Starting.Y) - C.Radius * C.Radius; Double u1 = ((-1) * b + System.Math.Sqrt(b * b - 4 * a * c)) / (2 * a); Double u2 = ((-1) * b - System.Math.Sqrt(b * b - 4 * a * c)) / (2 * a); //交点 PointD P1 = new PointD(L.Starting.X + u1 * (L.End.X - L.Starting.X), L.Starting.Y + u1 * (L.End.Y - L.Starting.Y)); PointD P2 = new PointD(L.Starting.X + u2 * (L.End.X - L.Starting.X), L.Starting.Y + u2 * (L.End.Y - L.Starting.Y)); if (LineAlgorithm.OnLine(L, P1) == true) { result.Add(P1); } if (LineAlgorithm.OnLine(L, P2) == true && P1.Equals(P2) == false) { result.Add(P2); } return(result.ToArray()); }
/// <summary> /// 判断矩形R是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="R">矩形R</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C, RectangleD R) { if (PointAlgorithm.Distance(new PointD(R.Left, R.Top), C.Center) > C.Radius) { return(false); } if (PointAlgorithm.Distance(new PointD(R.Right, R.Bottom), C.Center) > C.Radius) { return(false); } return(true); }
public void CircleD_OpEquality() { var circle1 = new CircleD(123.45, 456.78, 100.10); var circle2 = new CircleD(123.45, 456.78, 100.10); var circle3 = new CircleD(123.45, 555, 100.10); var circle4 = new CircleD(222, 456.78, 100.10); var circle5 = new CircleD(123.45, 456.78, 200); TheResultingValue(circle1 == circle2).ShouldBe(true); TheResultingValue(circle1 == circle3).ShouldBe(false); TheResultingValue(circle1 == circle4).ShouldBe(false); TheResultingValue(circle1 == circle5).ShouldBe(false); }
public void CircleD_OpInequality() { var circle1 = new CircleD(123.45, 456.78, 100.10); var circle2 = new CircleD(123.45, 456.78, 100.10); var circle3 = new CircleD(123.45, 555, 100.10); var circle4 = new CircleD(222, 456.78, 100.10); var circle5 = new CircleD(123.45, 456.78, 200); TheResultingValue(circle1 != circle2).ShouldBe(false); TheResultingValue(circle1 != circle3).ShouldBe(true); TheResultingValue(circle1 != circle4).ShouldBe(true); TheResultingValue(circle1 != circle5).ShouldBe(true); }
/// <summary> /// 判断线段L是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="L">线段L</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C, LineD L) { //判断点是否在圆内: //计算圆心到该点的距离,如果小于等于半径则该点在圆内。 if (PointAlgorithm.Distance(L.Starting, C.Center) > C.Radius) { return(false); } if (PointAlgorithm.Distance(L.End, C.Center) > C.Radius) { return(false); } return(true); }
public void CircleD_TryParse_SucceedsForValidStrings() { var str = "123.45 456.78 100.10"; var result = default(CircleD); if (!CircleD.TryParse(str, out result)) { throw new InvalidOperationException("Unable to parse string to CircleD."); } TheResultingValue(result) .ShouldHavePosition(123.45, 456.78) .ShouldHaveRadius(100.10); }
/// <summary> /// 判断折线PL是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="PL">折线PL</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C, PolylineD PL) { if (PL.Points == null) { return(false); } for (Int32 i = 0; i < PL.Points.Count; ++i) { if (PointAlgorithm.Distance(PL.Points[i], C.Center) > C.Radius) { return(false); } } return(true); }
/// <summary> /// 判断多边形PG是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="PG">多边形PG</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C, PolygonD PG) { if (PG.Vertex == null) { return(false); } for (Int32 i = 0; i < PG.Vertex.Count; ++i) { if (PointAlgorithm.Distance(PG.Vertex[i], C.Center) > C.Radius) { return(false); } } return(true); }
/// <summary> /// 判断圆C是否在多边形PG内 /// </summary> /// <param name="PG">多边形PG</param> /// <param name="C">圆C</param> /// <returns>如果圆C在区域内返回True,否则返回False.</returns> public static Boolean InPolygon(PolygonD PG, CircleD C) { //如果圆心不在多边形内则返回不在多边形内 if (false == InPolygon(PG, C.Center)) { return(false); } Double D = PointAlgorithm.ClosestDistance(C.Center, PG); if (D > C.Radius || DoubleAlgorithm.Equals(D, C.Radius)) { return(true); } return(false); }
/// <summary> /// 圆形是否在矩形内 /// </summary> /// <param name="R">矩形R</param> /// <param name="C">圆形C</param> /// <returns> 返回True表示圆形C在区域内,返回False则不在区域内.</returns> public static Boolean InRectangle(RectangleD R, CircleD C) { //很容易证明,圆在矩形中的充要条件是:圆心在矩形中且圆的半径小于等于圆心到矩形四边的距离的最小值。 if (InRectangle(R, C.Center) == false) { return(false); } Double MinXDistance = System.Math.Min((C.X - R.Left), (R.Right - C.X)); Double MinYDistance = System.Math.Min((C.Y - R.Top), (R.Bottom - C.Y)); if (C.Radius <= MinXDistance && C.Radius <= MinYDistance) { return(true); } return(false); }
/// <summary> /// 判断线段L与圆C的交点个数 /// </summary> /// <param name="L">线段L</param> /// <param name="C">圆形C</param> /// <returns>相交返回交点数目,否则返回0</returns> public static Int32?HasIntersection(LineD L, CircleD C) { Int32 count = 0; //如果和圆C有交点首先是L到圆心的距离小于或等于C的半径 if (DoubleAlgorithm.Equals(PointAlgorithm.ClosestDistance(C.Center, L), C.Radius)) { return(1); } else if (PointAlgorithm.ClosestDistance(C.Center, L) > C.Radius) { return(0); } if (PointAlgorithm.Distance(C.Center, L.Starting) >= C.Radius) { ++count; } if (PointAlgorithm.Distance(C.Center, L.End) >= C.Radius) { ++count; } return(count); }
/// <summary> /// Initializes a new instance of the <see cref="CircleDResult"/> class. /// </summary> /// <param name="value">The value being examined.</param> internal CircleDResult(CircleD value) { this.value = value; }
/// <summary> /// Wraps the specified unit test result for evaluation. /// </summary> /// <param name="value">The value to wrap.</param> /// <returns>The wrapped value.</returns> protected static CircleDResult TheResultingValue(CircleD value) { return new CircleDResult(value); }
/// <summary> /// 计算圆形面积 /// </summary> /// <param name="C">圆形C</param> /// <returns>返回面积。</returns> public static Double Area(CircleD C) { //formula PI*R*R return(System.Math.PI * C.Radius * C.Radius); }
/// <summary> /// 判断圆C2是否在圆C1内 /// </summary> /// <param name="C1">圆C1</param> /// <param name="C2">圆C2</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C1, CircleD C2) { //formula //C2的中心点到C1中心点的距离 加上C2的半径小于C1的半径 return(((PointAlgorithm.Distance(C1.Center, C2.Center) + C2.Radius) > C1.Radius) ? false : true); }
/// <summary> /// Wraps the specified unit test result for evaluation. /// </summary> /// <param name="value">The value to wrap.</param> /// <returns>The wrapped value.</returns> protected static CircleDResult TheResultingValue(CircleD value) { return(new CircleDResult(value)); }
/// <summary> /// 计算圆形的偏移 /// </summary> /// <param name="C">圆形C</param> /// <param name="velocity">偏移速度。</param> /// <returns>返回偏移后的圆形。</returns> public static CircleD Offset(CircleD C, PointD velocity) { return(new CircleD(C.Center + velocity, C.Radius)); }
public static void RenderFriendshipSymbols() { var ponyColors = ClassifyJson.DeserializeFile <Dictionary <string, string> >(_poniesJson); var filesButNoPonyColor = new DirectoryInfo(_poniesDir).EnumerateFiles("*.png").Where(f => !ponyColors.ContainsKey(Path.GetFileNameWithoutExtension(f.Name))).ToArray(); if (filesButNoPonyColor.Length > 0) { Console.WriteLine("Files but no pony color:"); foreach (var file in filesButNoPonyColor) { Console.WriteLine(Path.GetFileNameWithoutExtension(file.Name)); } Console.WriteLine(); } //ClassifyJson.SerializeToFile(ponyColors, _poniesJson); ponyColors.Where(kvp => File.Exists(Path.Combine(_poniesDir, $"{kvp.Key}.png"))).ParallelForEach(4, kvp => { var pony = kvp.Key; var newFilename = $"{kvp.Key}.png"; lock (ponyColors) Console.WriteLine("Starting " + newFilename); var color = Color.FromArgb(Convert.ToInt32(kvp.Value.Substring(0, 2), 16), Convert.ToInt32(kvp.Value.Substring(2, 2), 16), Convert.ToInt32(kvp.Value.Substring(4, 2), 16)); var newCutieMark = GraphicsUtil.MakeSemitransparentImage(200, 200, g => { g.SetHighQuality(); }, g => { g.Clear(color); using (var bmp = new Bitmap(Path.Combine(_poniesDir, $"{pony}.png"))) { var width = bmp.Width; var height = bmp.Height; var pts = new List <PointD>(); unsafe { var bits = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); for (int y = 0; y < height; y++) { byte *readFrom = (byte *)bits.Scan0 + y * bits.Stride; for (int x = 0; x < width; x++) { if (readFrom[4 * x + 3] != 0) { pts.Add(new PointD(x, y)); } } } bmp.UnlockBits(bits); } var circum = CircleD.GetCircumscribedCircle(pts); using (var tr = new GraphicsTransformer(g).Translate(-circum.Center.X, -circum.Center.Y).Scale(90 / circum.Radius, 90 / circum.Radius).Translate(100, 100)) { g.DrawImage(bmp, 0, 0); //g.DrawEllipse(Pens.Black, circum.ToRectangle().ToRectangleF()); } } }, g => { g.Clear(Color.Transparent); g.FillEllipse(Brushes.Black, 1, 1, 197, 197); }); //* var tmp = $@"D:\c\KTANE\Public\HTML\img\Friendship\tmp_{newFilename}"; var final = $@"D:\c\KTANE\Public\HTML\img\Friendship\{newFilename}"; newCutieMark.Save(tmp, ImageFormat.Png); CommandRunner.Run("pngcr", tmp, final).OutputNothing().Go(); File.Delete(tmp); /*/ * var final = $@"D:\c\KTANE\Friendship\Manual\img\Friendship\{newFilename}"; * newCutieMark.Save(final, ImageFormat.Png); * /**/ lock (ponyColors) Console.WriteLine("Saved " + newFilename); }); }
/// <summary> /// 计算点到圆的距离 /// </summary> /// <param name="C">圆C</param> /// <param name="P">点P</param> /// <returns>返回点到圆周的距离。</returns> /// <remarks> /// 返回值小于0 表示点在圆内。 /// 返回值等于0 表示点在圆周上。 /// 返回值大于0 表示点在圆外。 /// </remarks> public static Double Distance(CircleD C, PointD P) { return(PointAlgorithm.Distance(C.Center, P) - C.Radius); }
public void CircleD_Parse_FailsForInvalidStrings() { Assert.That(() => CircleD.Parse("foo"), Throws.TypeOf <FormatException>()); }
/// <summary> /// 计算线L到圆C的距离 /// </summary> /// <param name="C">圆C</param> /// <param name="L">线L</param> /// <returns>返回线到圆周的距离。</returns> /// <remarks> /// 返回值小于0 表示线在圆内或与圆周相交。 /// 返回值等于0 表示线在圆周上与圆周相切。 /// 返回值大于0 表示线在圆外与圆周没有交点。 /// </remarks> public static Double Distance(CircleD C, LineD L) { return(PointAlgorithm.Distance(C.Center, L) - C.Radius); }
/// <summary> /// 判断点P是否在圆边界上 /// </summary> /// <param name="C">圆C</param> /// <param name="P">点P</param> /// <returns>如果在圆边界上返回True,否则返回False。</returns> public static Boolean OnCircle(CircleD C, PointD P) { return(DoubleAlgorithm.Equals(Distance(C, P), 0)); }