dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: Set Sided Polygon in C++

  1. #16
    Join Date
    Oct 2019
    Posts
    5

    Re: Set Sided Polygon in C++

    Code:
    void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R, Color PixelColour )
    {
    	int x = X;
    	int y = Y;
    	int r = R;
    	int sides = Sides;
    
    	float cir = PI * (r * 2);
    
    	float edgelength = cir / sides;
    	float tempsides = 0;
    
    	float angle = 360 / sides;
    
    	while(tempsides < sides)
    	{
          		SetViewportPixel(x + r, y + r, PixelColour);
    		r = r + angle;
    		tempsides++;
    	}
    }
    Last edited by VictorN; October 17th, 2019 at 04:24 AM. Reason: added code tags

  2. #17
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: Set Sided Polygon in C++

    Did you debug this code?
    Victor Nijegorodov

  3. #18
    Join Date
    Oct 2019
    Posts
    5

    Re: Set Sided Polygon in C++

    Yes, Is there any error in code?

  4. #19
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: Set Sided Polygon in C++

    Quote Originally Posted by basilmon View Post
    Yes, Is there any error in code?
    Usually the compiler shows you all syntax errors.
    To find out and fix the design error one has to use the debugger!
    Victor Nijegorodov

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: Set Sided Polygon in C++

    Your code in post #16 is based upon the old code from post #1 - which is incorrect. As a minimum, it should be based upon the code from post #8. From your code,

    - edgelength isn't the edge length but the length of the sector arc.
    - tempsides should be int not float as sides is a whole number.

    assuming R is the radius length of the circumscribed circle and X, Y the centre co-ordinates of the circle, then you need to first determine the initial point on the circle then for each loop, r doesn't change but x and y do based upon angle.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)