Drawing Lines Using Line Equations

Environment: VC6

Often, in developing educational-related applications, one needs to draw lines in XY axes, and find the slope of that line. This article introduces how to draw lines in XY axes and find the slope of the line.



Click here for a larger image.

The application created here uses the dialog-based MFC AppWizard (EXE). The application has a TextBox in which you have to enter the line equation. The line equation must be in the 'AX+BY+C=0' format, where A, B, and C are integers. Entering =0 is not necessary. =0 is assumed by default.

The picture control is used to display the XY axes and to draw lines based on the equation provided in the TextBox. After you enter the line and click the button with name "TestLine", the slope of the line equation is displayed in degrees and radians when line equations such as x=0 and y=0 are entered. The details of that line are provided under the "Message" static control.

To display XY axes in the picture control, the code is written in the DrawXYAxis() function. The mapping mode is MM_LOMETRIC and the function used to set the mapping mode is SetMapMode(MM_LOMETRIC). The units are taken as 0.01mm.and 100mm=1cm., i.e 1unit=0.01mm., and the origin is changed to the center of the picture control with the SetViewportOrg() function.

If the LineEquation is AX+BY+C, then 'A' is Coefx (coefficient of X), B is Coefy (coefficient of Y), and C is Coefcons (Constant). A, B, and C are any Inter values.

The main functionality of drawing a line and finding the slope of the line is written in OnButtonTestLine(). To draw a line such as AX+BY+C=0, we choose two points in the X axis and the respective points are calculated on the Y axis. The X axis points are chosen as [-300,300]. The Y axis points are cleated as the following:

  float x1[2]={-300,300};
  float y11=((coefx*x1[0])/coefy)+(coefcons/coefy);
  float y22=((coefx*x1[1])/coefy)+(coefcons/coefy);
  float y1[2]={y11,y22};

The line is drawn between points (x1[0],y1[0]) and (x1[1],y1[1]) using the MoveTo() and LineTo() functions.

  dc->MoveTo(x1[0],y1[0]);
  dc->LineTo(x1[1],y1[1]);

To draw a current line in PictureControl and erase the previous line, the InvertLine() function is used. It draws a line, not by setting each pixel to a certain color, but by inverting the existing pixel colors. This ensures that the line can be seen no matter what background it is drawn against and that drawing the line again in the same location will erase it by restoring the original screen colors.

Downloads

Download demo project - 25 Kb
Download source - 13 Kb


Comments

  • nice equation parser, but...

    Posted by Legacy on 09/09/2003 12:00am

    Originally posted by: ahoodin

    Rarely at work is one given the equation of a line and asked to graph it. Typically, it is given as a line at an angle with some angle, endpoint coordinates and a length or it is given as two coordinates with the equation to be solved for by the user.
    This project might help in a math class.

    It would be more helpful to either request an angle and an endpoint or two endpoints and then to draw the line and return the equation.

    ahoodin

    Reply
  • Improper demo equation

    Posted by Legacy on 09/08/2003 12:00am

    Originally posted by: George Georgiev

    The demo equation (3x+4y+9=0) crosses X and Y at points -3 and -2.25 respectively. On the scale drawn, this is barely visible, and could mislead that the line actually crosses (0,0), which is not correct.

    Bigger value (e.g. 900) would show much better behaviour of this sample.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date