Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:09:04+00:00 2026-06-06T21:09:04+00:00

Scroll down to the edit to read part 2 So I am working on

  • 0

Scroll down to the edit to read part 2

So I am working on a project where I need to create a polygon using given X and Y coordinates. The coordinates a given in logical order and create a path. Now I need to calculate all positions of a poligon, if the path width would be [w] (for example 20 meters). We all know lines have no width.

This image explains a bit what I want to do:

Path

The black dots are the positions of the path, their X and Y coordinates are known.
The width of the red lines is known, they are all [w] (for example 20 meters, the path cuts them in the center).

I do not know how to get the X,Y positions of all the purple dots. I need them so I can create the green polygon shape.

How can I calculate these positions in C++? Are there any functions which make it easier?

P.S: As you see the red lines are angeled at the half of the angle of two blue lines.


Edit:

I created a visualisation app in Visual Basic .NET and I got the formulas which I can port to C++. There is still one problem, please look at this image:
enter image description here

(app download link: http://gpb.googlecode.com/files/DRAWER2.zip )

The problem now is that when the path turns, it reverses the sides on which the points of the polygon get created. This makes an corrupted polygon (or well, it doesn’t provide the wanted effect).

The code looks as following:

Dim MainImage As New DynamicBitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim temp As New DynamicBitmap
    MainImage.CreateGrid(500, 500, 1, 1)
    temp.LoadBitmap("map.jpg")
    MainImage.DrawOnSurface(temp.Bitmap, temp.Rectangle, MainImage.Rectangle)
    MainImage.Surface.DrawLine(Pens.Black, 0, 250, 500, 250)
    MainImage.Surface.DrawLine(Pens.Black, 250, 0, 250, 500)
    PictureBox1.Image = MainImage.Bitmap
    PictureBox1.Refresh()
End Sub
Dim CPG(0) As Point
Dim CurrCount As Integer = 0
Dim Distance As Double = 30.0
Function CalculatePositions(ByVal PointStart As Point, ByVal PointMiddle As Point, ByVal PointEnd As Point) As Point()

    Dim DeltaX As Double
    Dim DeltaY As Double
    Dim AdderX As Double
    Dim AdderY As Double
    Dim Length As Double
    Dim CP(9) As Point
    CP(0) = PointMiddle
    CP(1) = PointStart
    CP(2) = PointEnd
    Dim RetP(1) As Point
    DeltaX = CP(1).X - CP(0).X
    DeltaY = CP(1).Y - CP(0).Y
    Length = Math.Sqrt(((DeltaX * DeltaX) + (DeltaY * DeltaY))) + 0.0000000001
    AdderX = (DeltaX / Length)
    AdderY = (DeltaY / Length)
    CP(3).X = (CP(0).X + (AdderX * Distance))
    CP(3).Y = (CP(0).Y + (AdderY * Distance))

    DeltaX = CP(2).X - CP(0).X
    DeltaY = CP(2).Y - CP(0).Y
    Length = Math.Sqrt(((DeltaX * DeltaX) + (DeltaY * DeltaY))) + 0.0000000001
    AdderX = (DeltaX / Length)
    AdderY = (DeltaY / Length)
    CP(4).X = (CP(0).X + (AdderX * Distance))
    CP(4).Y = (CP(0).Y + (AdderY * Distance))

    DeltaX = CP(3).X - CP(4).X
    DeltaY = CP(3).Y - CP(4).Y
    Length = Math.Sqrt(((DeltaX * DeltaX) + (DeltaY * DeltaY))) + 0.0000000001
    AdderX = (DeltaX / Length)
    AdderY = (DeltaY / Length)
    CP(8).X = (CP(4).X + (AdderX * Length / 2.0))
    CP(8).Y = (CP(4).Y + (AdderY * Length / 2.0))

    DeltaX = CP(8).X - CP(0).X
    DeltaY = CP(8).Y - CP(0).Y
    Length = Math.Sqrt(((DeltaX * DeltaX) + (DeltaY * DeltaY))) + 0.0000000001
    AdderX = (DeltaX / Length)
    AdderY = (DeltaY / Length)
    CP(7).X = (CP(0).X - (AdderX * Distance))
    CP(7).Y = (CP(0).Y - (AdderY * Distance))
    CP(9).X = (CP(0).X + (AdderX * Distance))
    CP(9).Y = (CP(0).Y + (AdderY * Distance))

    MainImage.Surface.DrawLine(Pens.Red, New Point(CP(7).X - 3, CP(7).Y - 3), New Point(CP(7).X + 3, CP(7).Y + 3))
    MainImage.Surface.DrawLine(Pens.Red, New Point(CP(7).X + 3, CP(7).Y - 3), New Point(CP(7).X - 3, CP(7).Y + 3))
    MainImage.Surface.DrawLine(Pens.Blue, New Point(CP(9).X - 3, CP(9).Y - 3), New Point(CP(9).X + 3, CP(9).Y + 3))
    MainImage.Surface.DrawLine(Pens.Blue, New Point(CP(9).X + 3, CP(9).Y - 3), New Point(CP(9).X - 3, CP(9).Y + 3))
    Return RetP
End Function

Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
    'MsgBox(DirectCast(e, MouseEventArgs).X.ToString() + ":" + DirectCast(e, MouseEventArgs).Y.ToString())

    ReDim Preserve CPG(CurrCount)
    'i -= 1
    CPG(CurrCount) = New Point(DirectCast(e, MouseEventArgs).X, DirectCast(e, MouseEventArgs).Y)
    MainImage.Surface.DrawLine(Pens.Yellow, New Point(CPG(CurrCount).X - 3, CPG(CurrCount).Y - 3), New Point(CPG(CurrCount).X + 3, CPG(CurrCount).Y + 3))
    MainImage.Surface.DrawLine(Pens.Yellow, New Point(CPG(CurrCount).X + 3, CPG(CurrCount).Y - 3), New Point(CPG(CurrCount).X - 3, CPG(CurrCount).Y + 3))
    CurrCount += 1
    If CurrCount = 1 Then

    Else
        If CurrCount = 2 Then
            Dim DeltaX As Double
            Dim DeltaY As Double
            Dim AdderX As Double
            Dim AdderY As Double
            Dim Length As Double
            DeltaX = CPG(CurrCount - 2).X - CPG(CurrCount - 1).X
            DeltaY = CPG(CurrCount - 2).Y - CPG(CurrCount - 1).Y
            Length = Math.Sqrt(((DeltaX * DeltaX) + (DeltaY * DeltaY))) + 0.0000000001
            AdderX = (DeltaX / Length)
            AdderY = (DeltaY / Length)
            Dim Temp(1) As Point
            Dim Angle01 As Double = Math.Atan2(CPG(CurrCount - 1).X - CPG(CurrCount - 2).X, CPG(CurrCount - 1).Y - CPG(CurrCount - 2).Y) * 180.0 / Math.PI
            Dim SinMin As Double
            Dim CosMin As Double
            SinMin = Math.Sin(((-Angle01) + 0.0) / 180.0 * Math.PI)
            CosMin = Math.Cos(((-Angle01) + 0.0) / 180.0 * Math.PI)
            Temp(0).X = CPG(CurrCount - 2).X + (CosMin * Distance) + AdderX * Distance
            Temp(0).Y = CPG(CurrCount - 2).Y + (SinMin * Distance) + AdderY * Distance
            Temp(1).X = CPG(CurrCount - 2).X - (CosMin * Distance) + AdderX * Distance
            Temp(1).Y = CPG(CurrCount - 2).Y - (SinMin * Distance) + AdderY * Distance
            MainImage.Surface.DrawLine(Pens.Red, New Point(Temp(0).X - 3, Temp(0).Y - 3), New Point(Temp(0).X + 3, Temp(0).Y + 3))
            MainImage.Surface.DrawLine(Pens.Red, New Point(Temp(0).X + 3, Temp(0).Y - 3), New Point(Temp(0).X - 3, Temp(0).Y + 3))
            MainImage.Surface.DrawLine(Pens.Blue, New Point(Temp(1).X - 3, Temp(1).Y - 3), New Point(Temp(1).X + 3, Temp(1).Y + 3))
            MainImage.Surface.DrawLine(Pens.Blue, New Point(Temp(1).X + 3, Temp(1).Y - 3), New Point(Temp(1).X - 3, Temp(1).Y + 3))
        End If
        MainImage.Surface.DrawLine(Pens.Blue, CPG(CurrCount - 2), CPG(CurrCount - 1))
        If CurrCount > 2 Then
            CalculatePositions(CPG(CurrCount - 3), CPG(CurrCount - 2), CPG(CurrCount - 1))
        End If
    End If

    PictureBox1.Image = MainImage.Bitmap
    PictureBox1.Refresh()

    'MsgBox(CP(i).ToString())
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim DeltaX As Double
    Dim DeltaY As Double
    Dim AdderX As Double
    Dim AdderY As Double
    Dim Length As Double
    DeltaX = CPG(CurrCount - 2).X - CPG(CurrCount - 1).X
    DeltaY = CPG(CurrCount - 2).Y - CPG(CurrCount - 1).Y
    Length = Math.Sqrt(((DeltaX * DeltaX) + (DeltaY * DeltaY))) + 0.0000000001
    AdderX = (DeltaX / Length)
    AdderY = (DeltaY / Length)
    Dim Temp(1) As Point
    Dim Angle01 As Double = Math.Atan2(CPG(CurrCount - 1).X - CPG(CurrCount - 2).X, CPG(CurrCount - 1).Y - CPG(CurrCount - 2).Y) * 180.0 / Math.PI
    Dim SinMin As Double
    Dim CosMin As Double
    SinMin = Math.Sin(((-Angle01) + 0.0) / 180.0 * Math.PI)
    CosMin = Math.Cos(((-Angle01) + 0.0) / 180.0 * Math.PI)
    Temp(0).X = CPG(CurrCount - 1).X + (CosMin * Distance) - AdderX * Distance
    Temp(0).Y = CPG(CurrCount - 1).Y + (SinMin * Distance) - AdderY * Distance
    Temp(1).X = CPG(CurrCount - 1).X - (CosMin * Distance) - AdderX * Distance
    Temp(1).Y = CPG(CurrCount - 1).Y - (SinMin * Distance) - AdderY * Distance
    MainImage.Surface.DrawLine(Pens.Red, New Point(Temp(0).X - 3, Temp(0).Y - 3), New Point(Temp(0).X + 3, Temp(0).Y + 3))
    MainImage.Surface.DrawLine(Pens.Red, New Point(Temp(0).X + 3, Temp(0).Y - 3), New Point(Temp(0).X - 3, Temp(0).Y + 3))
    MainImage.Surface.DrawLine(Pens.Blue, New Point(Temp(1).X - 3, Temp(1).Y - 3), New Point(Temp(1).X + 3, Temp(1).Y + 3))
    MainImage.Surface.DrawLine(Pens.Blue, New Point(Temp(1).X + 3, Temp(1).Y - 3), New Point(Temp(1).X - 3, Temp(1).Y + 3))

    PictureBox1.Image = MainImage.Bitmap
    PictureBox1.Refresh()
End Sub

How can I fix this issue? I want the red X-es on one side and the blue X-es on the other side, and I want it to not be dependable on how the path turns. How to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:09:06+00:00Added an answer on June 6, 2026 at 9:09 pm

    You an detect which side of a line a point is on.

    position = Math.Sign( (Bx-Ax)*(Y-Ay) - (By-Ay)*(X-Ax) )
    

    where (Ax,Ay) is the starting point, and (Bx,By) is the ending point of the line. (X,Y) is the point you want to check.

    If the position is positive, it is on the left side, negative on the right (0 on the line).

    With this you swap the pink dots if they are on the wrong side of the path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT : Scroll down to see the updated code. I would like to build
To scroll down my chat DIV I am using the following code which is
EDIT: Update - scroll down EDIT 2: Update - problem solved Some background information:
Scroll down to EDIT 1. This top bit is irrelevant now. This is my
I am using jQuery's scrollTo plugin to scroll up and down my page, using
The following code is working well to scroll down the complete page. But I
I want to scroll down the page to a certain point (height of #first_column
Is it possible to scroll down the left and right parts of a vertically
If you scroll down to the section 'Writing to Files and URLs' at this
When you scroll down to the bottom of http://www.dzone.com/links/ it automatically loads more links.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.