How would I make a 3,4,5 triangle print out in a GUI. I was using the drawline function, but I can't figure out how to make it so its between two coordinate points. I have some code like this to draw a line

// draw line

         coloredPen->Color = Color::Red;

         coloredPen->Width = 5;

         graphicsObject->DrawLine( coloredPen, 395, 150, 250, 150 );

But I would like to use coordinate points to do this. I dont really know what to do from here.

Recommended Answers

All 8 Replies

Isn't that drawing from the point (395,150) to (250, 150)?

I've never used this before but if you were to make a 3, 4 ,5 triangle you could probably just go

graphicsObject->DrawLine( coloredPen, 200, 150, 200, 190 );
graphicsObject->DrawLine( coloredPen, 200, 190, 230, 190 );
graphicsObject->DrawLine( coloredPen, 230, 190, 200, 150 );

ok, thank you so much, thats what I was looking for. But I now have another question. How can I re-scale the window so I dont have to use points like (200,15). Because when I use points like (1,5), it is really , really small.

Does it have anything to do with the autocale function? Mine is at (5,13) right now.

It's small because its using pixel coordinates. If you want to use a custom coordinate system you will have to get the size of the drawing area (in pixels) then you pick how many units you want across the drawing area and then in your drawing function you convert your units back into pixels so it can draw to scale.

Ok, and how exactly would I do that. I am really new to the GUI stuff. And c++ in general.

Say your drawing space is 500 by 500 pixels. You want your coordinate system to be 10 by 10. So you would take 500 (x pixels) and divide it by 10 (x units) and that means that for every 1 unit you have 50 pixels on the x axis. Do the same thing for the y axis.

Then you can make a function that takes in your unit coordinates and transfers them into pixels.

void Drawing(int x1, int y1, int x2, int y2, DRAWINGTYPE graphicsObject, PENTYPE pen)
{
	graphicsObject.DrawLine( pen, (drawAreaWidth/myUnitsWidth)*x1, (drawAreaHeight/myUnitsHeight)*y1, (drawAreaWidth/myUnitsWidth)*x2, (drawAreaHeight/myUnitsHeight)*y2 );
}

If you were to make your graphicsObject and pen variables global then you wouldn't have to pass them into the function.

DRAWINGTYPE and PENTYPE are placeholders for whatever they are actually called since I have no clue what they are.

Ok this is my form.h, so where exactly would I put that code?

using namespace System;
   using namespace System::ComponentModel;
   using namespace System::Collections;
   using namespace System::Windows::Forms;
   using namespace System::Data;
   using namespace System::Drawing;
   using namespace System::Drawing::Drawing2D;

   /// <summary> 
   /// Summary for Form1
   ///
   /// WARNING: If you change the name of this class, you will need to change the 
   ///          'Resource File Name' property for the managed resource compiler tool 
   ///          associated with all .resx files this class depends on.  Otherwise,
   ///          the designers will not be able to interact properly with localized
   ///          resources associated with this form.
   /// </summary>
   public __gc class Form1 : public System::Windows::Forms::Form
   {   
   public:
      Form1(void)
      {
         InitializeComponent();
      }


   protected:
      void Dispose(Boolean disposing)
      {
         if (disposing && components)
         {
            components->Dispose();
         }
         __super::Dispose(disposing);
      }

   private:
      /// <summary>
      /// Required designer variable.
      /// </summary>
      System::ComponentModel::Container * components;

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      void InitializeComponent(void)
      {
		  this->SuspendLayout();
		  // 
		  // Form1
		  // 
		  this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
		  this->ClientSize = System::Drawing::Size(287, 195);
		  this->Name = S"Form1";
		  this->Text = S"Drawing Shapes";
		  this->ResumeLayout(false);

	  }   

   protected:

      // draw various shapes on form
      void OnPaint( PaintEventArgs *paintEvent )
      {
         __super::OnPaint( paintEvent ); // call base OnPaint method

         // pointer to object we will use
         Graphics *graphicsObject = paintEvent->Graphics;


         // bitmap texture
         Bitmap *textureBitmap = new Bitmap( 10, 10 );

         // get bitmap graphics
         Graphics *graphicsObject2 = Graphics::FromImage( textureBitmap );

         // brush and pen used throughout program
         SolidBrush *solidColorBrush = new SolidBrush( Color::Red );
         Pen *coloredPen = new Pen( solidColorBrush );



		 // draw green line
         coloredPen->Color = Color::Green;
         coloredPen->Width = 2;
        // graphicsObject->DrawLine( coloredPen, 320, 30, 250, 150);
		 graphicsObject->DrawLine( coloredPen, 150, 30, 150, 70);

	 // draw red line
         coloredPen->Color = Color::Red;
         coloredPen->Width = 2;
         //graphicsObject->DrawLine( coloredPen, 395, 150, 250, 150 );
		 graphicsObject->DrawLine( coloredPen, 150, 70, 180,70 );

         // draw yellow line
         coloredPen->Color = Color::Yellow;
         //graphicsObject->DrawLine( coloredPen, 320, 30, 395, 150 );
		 graphicsObject->DrawLine( coloredPen, 180, 70, 150, 30 );
      } // end method OnPaint        
   };
}

Nothing you posted there shows the name of your window handle or any other information I need.

I have never used .NET so I'm not sure if you can use GetClientRect(hWnd, windowRect); where hWnd is the handle for the window and windowRect is a RECT structure that will hold the left top right and bottom coordinates of the window.

So basically, there is no easy way to do this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.