How to Draw Static or Dynamic graphs like those?

旧城冷巷雨未停 提交于 2019-12-11 12:34:04

问题


I have been learning from this site for really long time, but this is actually my first appearance here, that is my first question, I am a civil Engineer and i am writing a software for the design of Retaining Walls, I need to draw figures like these shown below ,to be static or dynamic , (i mean by dynamic that i can change the dimensions of the graph via up-down numeric tool and the changes are visible at the graph,), these should be a ready forms to help the user entering the demotions.where i can find a source to learn this? i know i may be dealing with the smartest people in programing so please note that programing is not my major , i am not that good , consider me as a beginner ..thank you so much


回答1:


Here is a short introduction to the things you need to add drawings to your program:

0 - Decide on the platform: Winforms or WPF? For your kind of graphics WinForms will do; for 3D and animation WFP would be the better choice but at the cost of a steeper learning curve.

1 - Understand the basic graphics model in Winforms:

  • All things drawn onto a control must be drawn in the paint event or be triggered from there

  • Windows will take care of refreshing the drawing whenever outside events have e.g. covered it.

  • The user should be able to trigger the paint event by invalidating the control whenever he has changed the data. Add a refresh button with sketchPanel.Invalidate(); as its code.

2 - Add a control to draw on to the form and anchor or dock it as you like. Use a panel for drawing on, give it a nice name, say sketchPanel and create its Paint event ( by doubleclicking it in the event properties pane)

3 - All drawing operations happen on a Graphics object. In the Paint event it is provided as e.Graphics. If you would rather draw in a function of its own, say public drawMyWall(Graphics G) you can call it in the Paint event and pass in the e.Graphics object.

4 - The methods to use are

  • DrawLine for creating lines
  • DrawString for writing the measurement labels
  • FillPolygon for drawing a colored wall or the earth or water bodies
  • DrawPolygon for drawing an outline of the wall

All will take coordinates in pixel units relative to the sketchPanel; as your user input will be in meters or millimeters you will have to convert the numbers. Calculate the outer wall dimensions and scale them to the panel's size!

The Draw-/FillPloygon methods also expect an array of Points.

To create it you should first declare a List<Point> wallpoints = new List<Point>(); and then add each point you need: wallpoints.Add(new Point(someX, someY) ); Here both the order must be followed (CW or CCW) and the coordinates must each be calculated from the input measurements for each Point. A simple Gravity Wall will have only 4 points but the more complicated walls will have a dozen or more..

When your List is complete you can use in in e.g. the FillPolygon method like this:

e.Graphics.FillPolygon(Brushes.Orange, wallpoints.ToArray() );

Since you calculate all points from the input measurements the drawing will be completely dynamic.

You may want to leave out labels etc. at first. Also you may want to add an offset to the points you calculate to move the drawing to a more or less centered position on the panel.

I hope that helps..



来源:https://stackoverflow.com/questions/23701175/how-to-draw-static-or-dynamic-graphs-like-those

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!