/** * Description: * + draw rectangle to canvas at posX, posY * @Param: rectangle * @Param: posX * @Param: posY * Return */ private void drawRectangleToCanvas(Rectangle rec, double posX, double posY) { if (rec != null) { //if canvas contain object image then move positon if (cvMap.Children.Contains(rec)) { //create info of image _recInfo = rec.Tag as RectangleInfo; string recID = _recInfo.recID; double posCenterX = posX + rec.Width / 2; double posCenterY = cvMap.Height - posY - rec.Height / 2; _recInfo = new RectangleInfo(recID, posCenterX, posCenterY, rec.Height, rec.Width); rec.Tag = _recInfo; //update posotion Canvas.SetLeft(rec, posX); Canvas.SetTop(rec, posY); } else { string recID = rec.Tag.ToString(); double posCenterX = posX + rec.Width / 2; double posCenterY = cvMap.Height - posY - rec.Height / 2; _recInfo = new RectangleInfo(recID, posCenterX, posCenterY, rec.Height, rec.Width); rec.Tag = _recInfo; // cvMap.Children.Add(rec); Canvas.SetLeft(rec, posX); Canvas.SetTop(rec, posY); } } }
private static RectangleInfo GetRectangleFomContour(VectorOfPoint contour) { var minX = contour[0].X; var maxX = contour[0].X; var minY = contour[0].Y; var maxY = contour[0].Y; //Loop points and find max and min for (int PointNo = 0; PointNo < contour.Size; PointNo++) { if (contour[PointNo].X < minX) { minX = contour[PointNo].X; } if (contour[PointNo].X > maxX) { maxX = contour[PointNo].X; } if (contour[PointNo].Y < minY) { minY = contour[PointNo].Y; } if (contour[PointNo].Y > maxY) { maxY = contour[PointNo].Y; } } var rect = new RectangleInfo(minX, minY, maxX - minX, maxY - minY); return(rect); }
public void IClient_DrawRectangle(RectangleInfo rect, int color) { var p = new Message._IClient_DrawRectangle { rect = rect, color = color }; if (OnIClient_DrawRectangle != null) OnIClient_DrawRectangle(p); //Helper.Invoke(OnIClient_DrawRectangle, p); }
public RectangleInfo(RectangleInfo other) { this.Name = other.Name; this.X = other.X; this.Y = other.Y; this.Width = other.Width; this.Height = other.Height; this.Color = other.Color; }
public void Width_ShouldThrowException_WhenZeroOrNegativeNumber(int sign) { // Arrange var width = _fixture.Create <double>() * sign; // Act var rectangle = new RectangleInfo(); Exception exception = Record.Exception(() => rectangle.Width = width); // Assert Assert.NotNull(exception); Assert.IsType <ArgumentOutOfRangeException>(exception); }
public void Width_ShouldAcceptValue_WhenPositiveNumber() { // Arrange var width = _fixture.Create <double>(); // Act var rectangle = new RectangleInfo { Width = width }; // Assert Assert.Equal(width, rectangle.Width); }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); switch (gesture.GestureType) { case GestureType.FreeDrag: if (!isDragging) { RectangleInfo rectInfo = new RectangleInfo(); rectInfo.point1 = gesture.Position; rectInfo.point2 = gesture.Position; rectInfo.color = new Color(rand.Next(256), rand.Next(256), rand.Next(256)); rectangles.Add(rectInfo); isDragging = true; } else { RectangleInfo rectInfo = rectangles[rectangles.Count - 1]; rectInfo.point2 = gesture.Position; rectangles[rectangles.Count - 1] = rectInfo; } break; case GestureType.DragComplete: if (isDragging) { isDragging = false; } break; } } base.Update(gameTime); }
private void DXRibbonWindow_KeyDown(object sender, KeyEventArgs e) { if (this._clickRecObj != null) { int posY = (int)Canvas.GetTop(this._clickRecObj); int posX = (int)Canvas.GetLeft(this._clickRecObj); if (e.Key == Key.Left) { if (this._clickRecObj.Width > _temp) { this._clickRecObj.Width -= _temp; } } else if (e.Key == Key.Right) { if (posX + this._clickRecObj.Width < this.cvMap.Width) { this._clickRecObj.Width += _temp; } } else if (e.Key == Key.Up) { if (this._clickRecObj.Height > _temp) { this._clickRecObj.Height -= _temp; } } else if (e.Key == Key.Down) { if (posY + this._clickRecObj.Height < (int)this.cvMap.Height) { this._clickRecObj.Height += _temp; } } _recInfo = _clickRecObj.Tag as RectangleInfo; string recID = _recInfo.recID; double posCenterX = posX + _clickRecObj.Width / 2; double posCenterY = cvMap.Height - posY - _clickRecObj.Height / 2; _recInfo = new RectangleInfo(recID, posCenterX, posCenterY, _clickRecObj.Height, _clickRecObj.Width); _clickRecObj.Tag = _recInfo; } }
private void ucDrawBoard_MouseHover(object sender, EventArgs e) { if (!m_mouseHoverMode) { return; } Point pMouse = PointToClient(Control.MousePosition); if (m_rects.Any(r => r.Bound.Contains(pMouse))) { RectangleInfo info = m_rects.First(r => r.Bound.Contains(pMouse)); tipMsg.Show(info.Name, this, info.Bound.Right, info.Bound.Bottom); } else { tipMsg.Hide(this); } }
private void InitTestRectangle() { m_rects.Clear(); Point pStart = new Point(10, 10); for (int i = 0; i < 40; i++) { if (i != 0 && i % 10 == 0) { pStart.X = 10; pStart.Y += 45; } RectangleInfo info = new RectangleInfo(); info.Name = string.Format("矩形{0}", i + 1); info.Bound.Location = pStart; m_rects.Add(info); pStart.X += 45; } }
//If was left click on the Rectangle and then was right click on another Rectangle, then between these Rectangles makes the Line public void OnPointerClick(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Left) { startRectangle = this; } else if (eventData.button == PointerEventData.InputButton.Right && startRectangle != null && startRectangle != gameObject && !_joinedLineExist) { GameObject newLine = Instantiate(linePrefab, new Vector3().normalized, Quaternion.identity, canvasTransform); JointController newJointController = newLine.GetComponent <JointController>(); newJointController.startRectangle = startRectangle.transform; newJointController.finishRectangle = transform; newJointController.startRectangleInfo = startRectangle; newJointController.finishRectangleInfo = this; startRectangle.listLines.Add(newJointController); listLines.Add(newJointController); } }
void Update() { if (Input.GetMouseButtonDown(0)) { Vector2 mouse = Input.mousePosition; Vector2 mouseGlob = Camera.main.ScreenToWorldPoint(mouse); Vector2 topLeftCorner = new Vector2(mouseGlob.x - _halfWidthRectangle, mouseGlob.y + _halfHeightRectangle); Vector2 bottomRightCorner = new Vector2(mouseGlob.x + _halfWidthRectangle, mouseGlob.y - _halfHeightRectangle); Collider2D obstructionCollider = Physics2D.OverlapArea(topLeftCorner, bottomRightCorner); if (obstructionCollider == null) { RandomImageColor(); GameObject rectangle = Instantiate(_prefab, mouseGlob, Quaternion.identity, _canvasTransform); RectangleInfo rectangleInfo = rectangle.GetComponent <RectangleInfo>(); rectangleInfo.linePrefab = _linePrefab; rectangleInfo.canvasTransform = _canvasTransform; } } }
private void ucDrawBoard_MouseMove(object sender, MouseEventArgs e) { if (m_mouseHoverMode) { return; } Point pMouse = e.Location; if (m_rects.Any(r => r.Bound.Contains(pMouse))) { if (this.Tag == null) { RectangleInfo info = m_rects.First(r => r.Bound.Contains(pMouse)); tipMsg.Show(info.Name, this, info.Bound.Right, info.Bound.Bottom); this.Tag = info; } } else { tipMsg.Hide(this); this.Tag = null; } }
public void IServer_DrawRectangle(RectangleInfo rect, int color) { foreach (ServerSession v in this.OthersInTheLobby()) { v.IClient_DrawRectangle(rect, color); } }
// A rectange symdef is just a line symdef, but additional information is cached away. SymDef CreateRectangleSymdef(string name, int ocadID, OcadRectSymbol ocadSym) { // A rectangle symdef is just a line symbol with an additional RectangleInfo. SymColor color = GetColor(ocadSym.LineColor); float width = ToWorldDimensions(ocadSym.LineWidth); LineSymDef symdef = new LineSymDef(name, ocadID, color, width, ocadSym.Radius > 0 ? LineStyle.Rounded : LineStyle.Mitered); RectangleInfo rectinfo = new RectangleInfo(); rectinfo.cornerRadius = ToWorldDimensions(ocadSym.Radius); if ((ocadSym.GridFlags & 1) != 0) { // we have a grid. rectinfo.grid = true; CreateRectangleGridSymdefs(name, color, out rectinfo.gridLines, out rectinfo.gridText); rectinfo.numberFromBottom = ((ocadSym.GridFlags & 4) != 0) ? true: false; rectinfo.cellWidth = ToWorldDimensions(ocadSym.CellWidth); rectinfo.cellHeight = ToWorldDimensions(ocadSym.CellHeight); rectinfo.unnumberedCells = ocadSym.UnnumCells; rectinfo.unnumberedText = ocadSym.UnnumText; } // remember the rectangle info for later use in creating the symbol. rectangleInfos[ocadID] = rectinfo; return symdef; }
// Create the grid text symbols, void CreateGridText(SizeF size, Matrix matrix, float angle, int cxCells, int cyCells, float width, float height, RectangleInfo rectinfo, List<Symbol> symlist) { PointF[] pts = new PointF[1]; for (int y = 0; y < cyCells; ++y) for (int x = 0; x < cxCells; ++x) { int cellNum; string cellText; if (rectinfo.numberFromBottom) cellNum = y * cxCells + x + 1; else cellNum = (cyCells - 1 - y) * cxCells + x + 1; if (cellNum > cxCells * cyCells - rectinfo.unnumberedCells) cellText = rectinfo.unnumberedText; else cellText = cellNum.ToString(); pts[0] = new PointF((x + 0.07F) * width, (y + 1 - 0.04F) * height); pts[0].Y -= rectinfo.gridText.FontAscent - rectinfo.gridText.FontEmHeight; pts = GraphicsUtil.TransformPoints(pts, matrix); TextSymbol sym = new TextSymbol(rectinfo.gridText, new string[] {cellText}, pts[0], angle, 0); symlist.Add(sym); } }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); switch (gesture.GestureType) { case GestureType.FreeDrag: if (!isDragging) { RectangleInfo rectInfo = new RectangleInfo(); rectInfo.point1 = gesture.Position; rectInfo.point2 = gesture.Position; rectInfo.color = new Color(rand.Next(256), rand.Next(256), rand.Next(256)); rectangles.Add(rectInfo); isDragging = true; } else { RectangleInfo rectInfo = rectangles[rectangles.Count - 1]; rectInfo.point2 = gesture.Position; rectangles[rectangles.Count - 1] = rectInfo; } break; case GestureType.DragComplete: if (isDragging) isDragging = false; break; } } base.Update(gameTime); }
// Rectangle symbols may be translated into multiple symbols (if there is a grid). Symbol[] CreateRectangleSymbol(OcadObject obj, LineSymDef symdef, RectangleInfo rectinfo) { List<Symbol> symlist = new List<Symbol>(); // list of symbols we're creating. if (symdef == null) throw new OcadFileFormatException("Object has unknown or inconsistent symbol type {0}", obj.Sym); if (obj.coords == null || obj.coords.Length < 2) return null; // Create the main rectangle symbol. // Determine size of the rectangle, and matrix needed to transform points to their correct location. PointF[] pts = {PointFromOcadCoord(obj.coords[0]), PointFromOcadCoord(obj.coords[1]), PointFromOcadCoord(obj.coords[2]), PointFromOcadCoord(obj.coords[3])}; SizeF size = new SizeF(Util.DistanceF(pts[0], pts[1]), Util.DistanceF(pts[0], pts[3])); float angle = Util.Angle(pts[0], pts[1]); Matrix matrix = new Matrix(); matrix.Translate(pts[0].X, pts[0].Y); matrix.Rotate(angle); SymPath path; PointKind[] kinds; PointF[] pathpts; if (rectinfo.cornerRadius == 0) { kinds = new PointKind[] {PointKind.Corner, PointKind.Corner, PointKind.Corner, PointKind.Corner, PointKind.Corner}; pathpts = new PointF[] { new PointF(0,0), new PointF(0, size.Height), new PointF(size.Width, size.Height), new PointF(size.Width, 0), new PointF(0,0)}; } else { kinds = new PointKind[] {PointKind.Normal, PointKind.Normal, PointKind.BezierControl, PointKind.BezierControl, PointKind.Normal, PointKind.Normal, PointKind.BezierControl, PointKind.BezierControl, PointKind.Normal, PointKind.Normal, PointKind.BezierControl, PointKind.BezierControl, PointKind.Normal, PointKind.Normal, PointKind.BezierControl, PointKind.BezierControl, PointKind.Normal}; pathpts = new PointF[] { new PointF(rectinfo.cornerRadius, 0), new PointF(size.Width - rectinfo.cornerRadius, 0), new PointF(size.Width - (1-Util.kappa) * rectinfo.cornerRadius, 0), new PointF(size.Width, (1-Util.kappa) * rectinfo.cornerRadius), new PointF(size.Width, rectinfo.cornerRadius), new PointF(size.Width, size.Height - rectinfo.cornerRadius), new PointF(size.Width, size.Height - (1-Util.kappa) * rectinfo.cornerRadius), new PointF(size.Width - (1-Util.kappa) * rectinfo.cornerRadius, size.Height), new PointF(size.Width - rectinfo.cornerRadius, size.Height), new PointF(rectinfo.cornerRadius, size.Height), new PointF((1-Util.kappa) * rectinfo.cornerRadius, size.Height), new PointF(0, size.Height - (1-Util.kappa) * rectinfo.cornerRadius), new PointF(0, size.Height - rectinfo.cornerRadius), new PointF(0, rectinfo.cornerRadius), new PointF(0, (1-Util.kappa) * rectinfo.cornerRadius), new PointF((1-Util.kappa) * rectinfo.cornerRadius, 0), new PointF(rectinfo.cornerRadius, 0)}; } pathpts = GraphicsUtil.TransformPoints(pathpts, matrix); for (int i = 0; i < pathpts.Length; ++i) pathpts[i] = new PointF((float) Math.Round(pathpts[i].X, 2), (float) Math.Round(pathpts[i].Y, 2)); // round to 2 decimals, so round trip to OCAD without change. path = new SymPath(pathpts, kinds); symlist.Add(new LineSymbol(symdef, path)); if (rectinfo.grid) { if (size.Width > 0 && size.Height > 0) { int cxCells = (int) Math.Round(size.Width / rectinfo.cellWidth); if (cxCells < 1) cxCells = 1; int cyCells = (int) Math.Round(size.Height / rectinfo.cellHeight); if (cyCells < 1) cyCells = 1; float width = size.Width / cxCells; float height = size.Height / cyCells; CreateGridLines(size, matrix, cxCells, cyCells, width, height, rectinfo.gridLines, symlist); CreateGridText(size, matrix, angle, cxCells, cyCells, width, height, rectinfo, symlist); } } return symlist.ToArray(); }
/// <summary> /// point and radius are given in the unit specified by Scalemode /// </summary> public void Circle(PointF point, double radius, Color color, bool step) { if (step) { point.X = point.X + (float)CurrentX; point.Y = point.Y + (float)CurrentY; } double diameter = radius * 2; // Moves the CurrentX and CurrentY properties CurrentX = point.X + radius; CurrentY = point.Y + radius; //The final values are stored in pixels RectangleF circleRec = new RectangleF((float)ConvertToPrinterUnitsX(point.X - radius), (float)ConvertToPrinterUnitsY(point.Y - radius), (float)ConvertToPrinterUnitsX(diameter), (float)ConvertToPrinterUnitsY(diameter)); RectangleInfo circleInfo = new RectangleInfo(); circleInfo.Rec = circleRec; //circleInfo.Pen = _objPen; circleInfo.PenInfo = new PenInfo(this.DrawWidth, this.DrawStyle, color); circleInfo.FillColor = FillColor; circleInfo.FillStyle = _fillStyle; _bufferPage.AddCircle(circleInfo); }
/// <summary> /// 绘制地图 /// </summary> /// <param name="place">地点信息</param> /// <param name="mapgis">地图</param> private void DrawMap(ActivityPlaceInfo place, MapGIS mapgis = null) { GraphicInfo ginfo = getDrawInfo(place.Graphics, mapgis); if (ginfo is PolylineInfo) { PolylineInfo g = ginfo as PolylineInfo; if (g == null) { return; } mapgis.DrawPolyline(g.Points, place.Guid + "-0", new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), }, null); } if (ginfo is PolygonInfo) { PolygonInfo g = ginfo as PolygonInfo; if (g == null) { return; } mapgis.DrawPolygon(g.Points, place.Guid + "-0", new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } if (ginfo is CircleInfo) { CircleInfo g = ginfo as CircleInfo; if (g == null) { return; } mapgis.DrawCircle(g.CenterPoint, g.Radius, place.Guid + "-0" + "", new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } if (ginfo is RectangleInfo) { RectangleInfo g = ginfo as RectangleInfo; if (g == null) { return; } if (g.RightBottomPoint != null) { mapgis.DrawRectangle(g.LeftTopPoint, g.RightBottomPoint, place.Guid + "-0", new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } else { mapgis.DrawRectangle(g.LeftTopPoint, g.RectWidth, g.RectHeight, place.Guid + "-0", new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } } }
/// <summary> /// 根据字符串获取图形类信息 /// </summary> /// <param name="info"></param> /// <returns></returns> public static GraphicInfo getDrawInfo(string info, MapGIS mapgis) { if (mapgis == null) { return(null); } info = info.Replace(" ", "").Replace("\n", ""); string[] gInfoArr = null; string[] gInfo = info.Split(':'); if (gInfo.Length == 2) { switch (gInfo[0]) { case "polyline": PolylineInfo lineInfo = new PolylineInfo(); gInfoArr = gInfo[1].Split('|'); if (gInfoArr.Length == 2) { //样式 string[] gStyle = gInfoArr[0].Split(','); if (gStyle.Length == 2) { lineInfo.BorderColor = gStyle[0]; double.TryParse(gStyle[1], out lineInfo.BorderWidth); } //点 string[] gPoints = gInfoArr[1].Split('&'); if (gPoints.Length > 0) { lineInfo.Points = new List <I_GS_MapBase.Portal.Types.MapPointEx>(); for (int i = 0; i < gPoints.Length; i++) { string[] p = gPoints[i].Split(','); lineInfo.Points.Add(mapgis.MainMap.MapPointFactory.Create(p[0].TryToDoubleZero(), p[1].TryToDoubleZero())); } } } return(lineInfo); case "polygon": PolygonInfo gonInfo = new PolygonInfo(); gInfoArr = gInfo[1].Split('|'); if (gInfoArr.Length == 2) { //样式 string[] gStyle = gInfoArr[0].Split(','); if (gStyle.Length == 3) { gonInfo.BorderColor = gStyle[0]; double.TryParse(gStyle[1], out gonInfo.BorderWidth); gonInfo.FillColor = gStyle[2]; } //点 string[] gPoints = gInfoArr[1].Split('&'); if (gPoints.Length > 0) { gonInfo.Points = new List <I_GS_MapBase.Portal.Types.MapPointEx>(); for (int i = 0; i < gPoints.Length; i++) { string[] p = gPoints[i].Split(','); gonInfo.Points.Add(mapgis.MainMap.MapPointFactory.Create(p[0].TryToDoubleZero(), p[1].TryToDoubleZero())); } } } return(gonInfo); case "circle": CircleInfo circleInfo = new CircleInfo(); gInfoArr = gInfo[1].Split('|'); if (gInfoArr.Length == 2) { //样式 string[] gStyle = gInfoArr[0].Split(','); if (gStyle.Length == 3) { circleInfo.BorderColor = gStyle[0]; double.TryParse(gStyle[1], out circleInfo.BorderWidth); circleInfo.FillColor = gStyle[2]; } //中心点 半径 string[] gPoints = gInfoArr[1].Split('&'); if (gPoints.Length == 2) { string[] p = gPoints[0].Split(','); circleInfo.CenterPoint = mapgis.MainMap.MapPointFactory.Create(p[0].TryToDoubleZero(), p[1].TryToDoubleZero()); circleInfo.Radius = gPoints[1].TryToDoubleZero(); } } return(circleInfo); case "rectangle": RectangleInfo rectangleInfo = new RectangleInfo(); gInfoArr = gInfo[1].Split('|'); if (gInfoArr.Length == 2) { //样式 string[] gStyle = gInfoArr[0].Split(','); if (gStyle.Length == 3) { rectangleInfo.BorderColor = gStyle[0]; double.TryParse(gStyle[1], out rectangleInfo.BorderWidth); rectangleInfo.FillColor = gStyle[2]; } string[] gPoints = gInfoArr[1].Split('&'); if (gPoints.Length == 3) { string[] p = gPoints[0].Split(','); rectangleInfo.LeftTopPoint = mapgis.MainMap.MapPointFactory.Create(p[0].TryToDoubleZero(), p[1].TryToDoubleZero()); rectangleInfo.RectWidth = gPoints[1].TryToDoubleZero(); rectangleInfo.RectHeight = gPoints[2].TryToDoubleZero(); } if (gPoints.Length == 2) { string[] p = gPoints[0].Split(','); rectangleInfo.LeftTopPoint = mapgis.MainMap.MapPointFactory.Create(p[0].TryToDoubleZero(), p[1].TryToDoubleZero()); string[] p2 = gPoints[1].Split(','); rectangleInfo.RightBottomPoint = mapgis.MainMap.MapPointFactory.Create(p2[0].TryToDoubleZero(), p2[1].TryToDoubleZero()); } } return(rectangleInfo); } } return(null); }
private void RenderFrame(object sender, EventArgs e) { if (ellipses.Count == 0) { Random rand = new Random(); double spreadX; double spreadY; for (int i = 0; i < bulletPhysics.GetCountBullets(); i++) { if (rand.Next(1, 11) > 5) { spreadX = clickX - spread * rand.Next(50, 100); } else { spreadX = clickX + spread * rand.Next(50, 100); } if (rand.Next(1, 11) > 5) { spreadY = clickY - spread * rand.Next(50, 100); } else { spreadY = clickY + spread * rand.Next(50, 100); } Ellipse ellipse = new Ellipse(); ellipse.Fill = BrushesSelected(); // сделать ввод из комбобокса ellipse.Width = bulletPhysics.GetSizeBullet(); ellipse.Height = bulletPhysics.GetSizeBullet(); Canvas.SetLeft(ellipse, 125); Canvas.SetTop(ellipse, 125); Map.Children.Add(ellipse); EllipseInfo info = new EllipseInfo(ellipse, bulletPhysics.GetVelocity(), spreadX, spreadY); ellipses.Add(info); } for (int i = 0; i < 1; i++) { Rectangle rectangle = new Rectangle(); rectangle.Fill = Brushes.Black; rectangle.Width = 100; rectangle.Height = 100; Canvas.SetLeft(rectangle, 325); Canvas.SetTop(rectangle, 325); Map.Children.Add(rectangle); RectangleInfo info = new RectangleInfo(rectangle); wall.Add(info); } } else { for (int i = ellipses.Count - 1; i >= 0; i--) { EllipseInfo info = ellipses[i]; double left = Canvas.GetLeft(info.Ellipse); double top = Canvas.GetTop(info.Ellipse); double catet1 = Math.Abs(info.SpreadPathBulletX - left); double catet2 = Math.Abs(info.SpreadPathBulletY - top); double len = Math.Sqrt(catet1 * catet1 + catet2 * catet2); catet1 /= len; catet2 /= len; if (clickX > left) { Canvas.SetLeft(info.Ellipse, left + catet1 * info.VelocityY); } else if (left > clickX) { Canvas.SetLeft(info.Ellipse, left - catet1 * info.VelocityY); } if (clickY > top) { Canvas.SetTop(info.Ellipse, top + catet2 * info.VelocityY); } else if (top > clickY) { Canvas.SetTop(info.Ellipse, top - catet2 * info.VelocityY); } UIElementCollection children1 = Map.Children; var children = children1.OfType <UIElement>().ToList(); foreach (var recc in children) { if (recc is Rectangle) { Rectangle rec = recc as Rectangle; double wallY = Canvas.GetTop(rec); double wallX = Canvas.GetLeft(rec); double bulletY = Canvas.GetTop(info.Ellipse); double bulletX = Canvas.GetLeft(info.Ellipse); if ((wallY + 100 >= bulletY && wallY <= bulletY) && (wallX + 100 >= bulletX && wallX <= bulletX)) { Map.Children.Remove(info.Ellipse); ellipses.Remove(info); break; } else { if (info.SpreadPathBulletY + 16 + spread * 100 >= top && info.SpreadPathBulletY - 16 - spread * 100 <= top && info.SpreadPathBulletX + 16 + spread * 100 >= left && info.SpreadPathBulletX - 16 - spread * 100 <= left) { Map.Children.Remove(info.Ellipse); ellipses.Remove(info); } else if (bulletPhysics.GetBurstBullet() == true && bulletPhysics.GetCountBullets() > 1 && ellipses.Count == i) { info.VelocityY += accelerationY; } } } } if (ellipses.Count == 0) { StopRendering(); clickCanvasOn = true; } } } }
/// <summary> /// rec is given in the unit specified by Scalemode /// </summary> private void DrawRectangle(RectangleF rec, Color color, bool fill) { //Stores the position in pixels within the structure Rectangle RectangleF newRec = new RectangleF((float)ConvertToPrinterUnitsX(rec.X), (float)ConvertToPrinterUnitsY(rec.Y), (float)ConvertToPrinterUnitsX(rec.Width), (float)ConvertToPrinterUnitsY(rec.Height)); RectangleInfo newRectangle = new RectangleInfo(); newRectangle.Rec = newRec; //newRectangle.Pen = (Pen)_objPen.Clone(); //newRectangle.Pen.Color = color; newRectangle.PenInfo = new PenInfo((int)ConvertToPixelsX(this.DrawWidth), this.DrawStyle, color); if (fill) { newRectangle.FillStyle = FillStyleConstants.VbFsSolid; newRectangle.FillColor = color; } else { newRectangle.FillStyle = _fillStyle; newRectangle.FillColor = FillColor; } _bufferPage.AddRectangle(newRectangle); }
public void AddCircle(RectangleInfo coordinateInfo) { Dirty = true; if (this._circles == null) { this._circles = new List<RectangleInfo>(); } this._circles.Add(coordinateInfo); }
/// <summary> /// 解析字符串并绘制 /// </summary> /// <param name="GraphicId">id</param> /// <param name="GraphicInfo">图形信息 格式:polyline:blue,2|127,1,42.2&126.8&41.9</param> /// <param name="mapControl">需要绘制的地图对象,为空则往ShowMap上绘制</param> private void DrawPlaceLocation(string GraphicId, string GraphicInfo, MapGIS mapgis = null) { if (string.IsNullOrEmpty(GraphicId) || string.IsNullOrEmpty(GraphicInfo)) { return; } //各形状用*分隔 //polyline:blue,2|127,1,42.2&126.8,41.9 //polygon:blue,2,red|127,1,42.2&126.8,41.9 //circle:blue,2,red|127,1,42.2&2000 //rectangle:blue,2,red|127,1,42.2&127,1,42.2 //rectangle:blue,2,red|127,1,42.2&2000&1000 if (mapgis == null) { mapgis = ShowMap; } string[] info = GraphicInfo.Split('*'); for (int i = 0; i < info.Length; i++) { NameId++; GraphicInfo ginfo = getDrawInfo(info[i], mapgis); if (ginfo is PolylineInfo) { PolylineInfo g = ginfo as PolylineInfo; if (g == null) { continue; } mapgis.DrawPolyline(g.Points, GraphicId + "-" + NameId.ToString(), new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), }, null); } if (ginfo is PolygonInfo) { PolygonInfo g = ginfo as PolygonInfo; if (g == null) { continue; } mapgis.DrawPolygon(g.Points, GraphicId + "-" + NameId.ToString(), new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } if (ginfo is CircleInfo) { CircleInfo g = ginfo as CircleInfo; if (g == null) { continue; } mapgis.DrawCircle(g.CenterPoint, g.Radius, GraphicId + "-" + NameId.ToString(), new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } if (ginfo is RectangleInfo) { RectangleInfo g = ginfo as RectangleInfo; if (g == null) { continue; } if (g.RightBottomPoint != null) { mapgis.DrawRectangle(g.LeftTopPoint, g.RightBottomPoint, GraphicId + "-" + NameId.ToString(), new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } else { mapgis.DrawRectangle(g.LeftTopPoint, g.RectWidth, g.RectHeight, GraphicId + "-" + NameId.ToString(), new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(GraphicStyle.BorderColor.ToString(), g.BorderColor), new KeyValuePair <string, object>(GraphicStyle.BorderWidth.ToString(), g.BorderWidth), new KeyValuePair <string, object>(GraphicStyle.FillColor.ToString(), g.FillColor), }, null); } } } }
public ImageDimensionResponse ExtractDimensions(FileInfo imageFileInfo) { var imageDimensionResponse = new ImageDimensionResponse { Width = 0, Height = 0, CroppedImage = null, ImageWithDimensions = null }; double refObjectWidth = Properties.Settings.Default.RefObjectWidth; double refObjectHeight = Properties.Settings.Default.RefObjectHeight; //10, 20 eller 30 cm double minObjectArea = Properties.Settings.Default.MinObjectArea; //#load the image, convert it to grayscale, and blur it slightly //py: image = cv2.imread(args["image"]) var image = CvInvoke.Imread(imageFileInfo.FullName); //py: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) var gray = new UMat(); CvInvoke.CvtColor(image, gray, ColorConversion.Bgr2Gray); //py: gray = cv2.GaussianBlur(gray, (7, 7), 0) CvInvoke.GaussianBlur(image, gray, new System.Drawing.Size(9, 9), 0); //# perform edge detection, then perform a dilation + erosion to close gaps in between object edges //py: edged = cv2.Canny(gray, 50, 100) UMat edged = new UMat(); CvInvoke.Canny(gray, edged, 50, 100); IInputArray element = null; Point anchor = new Point(-1, -1); MCvScalar borderValue = new MCvScalar(); // py:edged = cv2.dilate(edged, None, iterations = 1) CvInvoke.Dilate(edged, edged, element, anchor, 1, Emgu.CV.CvEnum.BorderType.Default, borderValue); //py: edged = cv2.erode(edged, None, iterations = 1) CvInvoke.Erode(edged, edged, element, anchor, 1, BorderType.Default, borderValue); //# find contours in the edge map // py: cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) VectorOfVectorOfPoint cnts = new VectorOfVectorOfPoint(); CvInvoke.FindContours(edged.Clone(), cnts, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); // py: cnts = cnts[0] if imutils.is_cv2() else cnts[1] var cntsPoint = cnts[0]; //Obs! Kanske skall vara 1 isf 0 //Create dict that and add left X-coordinates for sorting var dict = new Dictionary <string, int>(); if (cnts.Size > 0) { for (int i = 0; i < cnts.Size; i++) { double area = CvInvoke.ContourArea(cnts[i]); var Rect = GetRectangleFomContour(cnts[i]); int xPos = Rect.X; int rectArea = 0; var leadingZero = "0000000000000" + xPos.ToString(); var fixedLength = leadingZero.Substring(leadingZero.Length - 8); //Treat small contours as far right if (area > 40) { var contourRect = GetRectangleFomContour(cnts[i]); rectArea = contourRect.Height * contourRect.Width; } //if ((rectArea < minObjectArea) ) //if ((rectArea < minObjectArea) || (Rect.Height < 500) || (Rect.Width < 150) ) if (rectArea < minObjectArea) { //Prefix too small objects with z to sort them after the interesting objects dict.Add("z" + fixedLength + "-" + i.ToString(), i); } else { //dict.Add(i, xPos); dict.Add(fixedLength + "-" + i.ToString(), i); } } } //var Sorteditems = dict.OrderBy(v => v.Value); var list = dict.Keys.ToList(); list.Sort(); //// Debug: Loop through keys. //foreach (var key in list) //{ // Console.WriteLine("{0}: {1}", key, dict[key]); // var itemNo = dict[key]; // var itemX = key; //} var objectToMeasureRect = new RectangleInfo(0, 0, 0, 0); int refHeightPx = 0; int refWidthPx = 0; int objectHeightPx = 0; int objectWidthPx = 0; int objectNo = 0; //# loop over the contours individually //for (int i = 0; i < cnts.Size; i++) foreach (var key in list) { //var rectNo = int.Parse(Sorteditems.ElementAt(i).Key.ToString()); var rectNo = dict[key]; var contour = cnts[rectNo]; //var Area = CvInvoke.ContourArea(Contour); var contourRect = GetRectangleFomContour(contour); var area = contourRect.Height * contourRect.Width; //Skip contours with small area if (area > minObjectArea) { objectNo++; ////Debug - Spara en kopia på varge contour //Rectangle rectCont = new Rectangle(contourRect.X, contourRect.Y, contourRect.Width, contourRect.Height); //var imageCont = image.Clone(); //var contImage = new Mat(imageCont, rectCont); //var fileName = imageFileInfo.FullName + "_rectNo" + rectNo.ToString() + "_i" + rectNo.ToString() + ".jpg"; //Console.WriteLine("bild " + fileName + " har X:" + rectCont.X.ToString()); //contImage.Save(fileName); var heightPx = contourRect.Height; var widthPx = contourRect.Width; if (objectNo == 1) { refHeightPx = heightPx; refWidthPx = widthPx; double AspectRatio = contourRect.AspectRatio; double RefAspectRatio = (double)refObjectWidth / (double)refObjectHeight; //Toto: Kolla att det är grönt //var avgColour = CvInvoke.Mean(contour); //Console.WriteLine("Colour: " + avgColour.V0.ToString() + ", " + avgColour.V1.ToString() + ", " + avgColour.V2.ToString() + ", " + avgColour.V3.ToString()); if (AspectRatio > (1.4 * RefAspectRatio) || AspectRatio < (0.9 * RefAspectRatio)) { objectNo = 0; //Todo Log and handle error //Console.WriteLine("Incorrect aspect ratio for reference object"); } //Debug, draw red lines //CvInvoke.Line(image, new Point(contourRect.X, contourRect.Y), new Point(contourRect.X + contourRect.Width, contourRect.Y), new Bgr(80, 80, 250).MCvScalar, 1); //CvInvoke.Line(image, new Point(rect[1].X, rect[1].Y), new Point(rect[2].X, rect[2].Y), new Bgr(80, 80, 250).MCvScalar, 1); if (objectNo == 1) { //Draw a line on left side of ref object CvInvoke.Line(image, contourRect.TopLeft, contourRect.BottomLeft, new Bgr(80, 180, 80).MCvScalar, 1); //Debug //Print Area //CvInvoke.PutText( // image, // "Area: " + Math.Round((Decimal)area, 0, MidpointRounding.AwayFromZero).ToString() + " Px2", // new Point(contourRect.X + 10, contourRect.Y + 50), // FontFace.HersheyComplexSmall, // 0.7, // new Bgr(90, 90, 255).MCvScalar); ////Print Aspect //CvInvoke.PutText( // image, // "Aspect: " + Math.Round((Decimal)RefAspectRatio, 2, MidpointRounding.AwayFromZero).ToString(), // new Point(contourRect.X + 10, contourRect.Y + 100), // FontFace.HersheyComplexSmall, // 0.7, // new Bgr(90, 90, 255).MCvScalar); } } //Check Flower Pot minimum height int minObjectHeight = Properties.Settings.Default.MinObjectHeight; int minObjectWidth = Properties.Settings.Default.MinObjectWidth; if (objectNo > 1 && objectNo < 5 && contourRect.Height > minObjectHeight && contourRect.Width > minObjectWidth) { objectHeightPx = heightPx; objectWidthPx = widthPx; objectToMeasureRect = contourRect; ////Debug ////Print Area //CvInvoke.PutText( // image, // "Area: " + Math.Round((Decimal)area, 0, MidpointRounding.AwayFromZero).ToString() + " Px2", // new Point(contourRect.X + 10, contourRect.Y + 50), // FontFace.HersheyComplexSmall, // 0.7, // new Bgr(90, 90, 255).MCvScalar); } } } double objectHeight = 0; double objectWidth = 0; if (objectNo > 1) { objectHeight = refObjectHeight * objectHeightPx / refHeightPx; //objectWidth = refObjectWidth * objectWidthPx / refWidthPx; //testa att använda höjden som referens. Lägg på 5% för att det oftast stämmer bättre. objectWidth = 1.05 * refObjectHeight * objectWidthPx / refHeightPx; //Crop image var cropX = (int)Math.Round(0.75 * objectToMeasureRect.X); var cropWidth = (int)(Math.Round(objectWidthPx * 1.9, 0)); if (cropWidth > (image.Width - cropX)) { cropWidth = image.Width - cropX; } var cropY = (int)Math.Round(0.8 * objectToMeasureRect.Y); var cropHeight = (int)(Math.Round(objectHeightPx * 1.5, 0)); if (cropHeight > (image.Height - cropY)) { cropHeight = image.Height - cropY; } Rectangle rectCrop = new Rectangle(cropX, cropY, cropWidth, cropHeight); var imageToCrop = image.Clone(); var croppedImage = new Mat(imageToCrop, rectCrop); //py: edged = cv2.erode(edged, None, iterations = 1) //Print Height CvInvoke.PutText( image, "ca: " + Math.Round((Decimal)objectHeight, 0, MidpointRounding.AwayFromZero).ToString() + " cm", new Point(objectToMeasureRect.X - 150, objectToMeasureRect.Y + (objectHeightPx) / 2), FontFace.HersheyComplexSmall, 1.0, new Bgr(80, 180, 80).MCvScalar); //Print width CvInvoke.PutText( image, "ca: " + Math.Round((Decimal)objectWidth, 0, MidpointRounding.AwayFromZero).ToString() + " cm", new Point(objectToMeasureRect.X - 50 + (objectWidthPx) / 2, objectToMeasureRect.Y - 20), FontFace.HersheyComplexSmall, 1.0, new Bgr(80, 180, 80).MCvScalar); var colour = new Bgr(80, 180, 80).MCvScalar; int lineThickness = 2; var rec = new RectangleInfo(objectToMeasureRect.X, objectToMeasureRect.Y, objectToMeasureRect.Width, objectToMeasureRect.Height); //Draw a frame around the measured object CvInvoke.Line(image, rec.TopLeft, rec.TopRight, colour, lineThickness); CvInvoke.Line(image, rec.TopRight, rec.BottomRight, colour, lineThickness); CvInvoke.Line(image, rec.BottomRight, rec.BottomLeft, colour, lineThickness); CvInvoke.Line(image, rec.BottomLeft, rec.TopLeft, colour, lineThickness); // CroppedImage = croppedImage.ToImage<Bgr, Byte>().ToBitmap(), // ImageWithDimensions = image.ToImage<Bgr, Byte>().ToBitmap() imageDimensionResponse.Width = objectWidth; imageDimensionResponse.Height = objectHeight; if (objectWidth > 0) { imageDimensionResponse.CroppedImage = croppedImage.ToImage <Bgr, Byte>().ToBitmap(); imageDimensionResponse.ImageWithDimensions = image.ToImage <Bgr, Byte>().ToBitmap(); } image.Dispose(); imageToCrop.Dispose(); return(imageDimensionResponse); } else { imageDimensionResponse.Width = objectWidth; imageDimensionResponse.Height = objectHeight; imageDimensionResponse.ErrorMessage = "Could not extract dimensions"; image.Dispose(); return(imageDimensionResponse); } }
public void IClient_DrawRectangle(RectangleInfo rect, int color) { }