[RESOLVED] How to get Equal Parts or Equal Radians in a Circle ?
Hello
Does anyone know how to get Equal parts or Equal Radians in a Circle for the below defined case?
Have attached image for your reference ? Img1
In this case I've tried to divide Circle by 16 parts somehow I am getting 17 parts.
Can anyone help me to achieve the Exact result of 16 parts. Even in the Image 1, between the two red line marked,
shows the extra 17th part.
How can I avoid showing the 17th part and therefore all the radians to be of Equal.
Below in code I've taken MOD 22, although 360 / 16 = 22.5. I don't know what needs to be taken as 22 or 22.5
Also I tried to fill the Radians. But did not come correctly. ie. because I could not match the correct points
Img2 is derived by below coding
So how to achieve as per image1, ie by Filling 8 radians alternately if 16 parts are equally divided
Img1(Could be untidy as edited in MS-Paint) .
Code:
Private newImg As New Bitmap(700, 700)
Private gr As Graphics = Graphics.FromImage(newImg)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cPoint = New Point(newImg.Width / 2, newImg.Width / 2)
Dim radiuss As Integer = newImg.Width / 2
EqualFacesInCircle(gr, cPoint, radiuss)
End Sub
Private Sub EqualFacesInCircle(ByVal gr As Graphics, ByVal centre As PointF, ByVal radius As Single)
Dim bluePen As New Pen(Color.Blue, 2)
For i As Integer = 0 To 359
Dim angle As Double = Math.PI * 2 * i / 360 ' Calculate angle for each line
' outer point of the line
Dim outerX As Single = centre.X + CSng(Math.Cos(angle) * radius)
Dim outerY As Single = centre.Y + CSng(Math.Sin(angle) * radius)
' Inner point of the tick mark
Dim lengthOfTickMark As Integer = 17.5
Dim innerRadius As Integer = If(i Mod 22 = 0, radius - lengthOfTickMark, Nothing)
Dim LongerInnerX As Single = centre.X + CSng(Math.Cos(angle) * innerRadius)
Dim LongerInnerY As Single = centre.X + CSng(Math.Sin(angle) * innerRadius)
'Dim sldBrBlue As New SolidBrush(Blue)
Dim lemonAppealBrush As SolidBrush = New SolidBrush(Color.FromArgb(239, 227, 175))
If i Mod 22 = 0 Then
gr.DrawLine(bluePen, New PointF(outerX, outerY), New PointF(centre.X, centre.Y))
Dim point1 As Point = New Point(centre.X, centre.Y) '(newXCentPoint, newYCentPoint) 'Centre (350,350)
Dim point2 As Point = New Point(centre.X, outerY) '(newXCentPoint, y1) '(350, 50) 'Top Centre
Dim point3 As Point = New Point(outerX, centre.Y) '(newXCentPoint + HalfEdgeSqWidth, newYCentPoint) '(650, 350) 'Centre to Right
Dim curvePoints1 As Point() = {point1, point2, point3}
gr.FillPolygon(lemonAppealBrush, curvePoints1)
End If
Next
PictureBox1.Image = newImg
End Sub
EDIT: I tried to fill up color for all the radians
Your help will be appreciated.
Re: How to get Equal Parts or Equal Radians in a Circle ?
There's an old saying, Calculus is another opportunity to fail Trig. Graphics Programming is also an opportunity to fail Trig.
Your For loop and inner logic seem poorly thought out. If all you want to do is draw something every n Radians, then the step of your For loop should be n Radians.
This would be my recommended For loop beginning:
Code:
' Defining this here for clarity. You may want this to be a parameter passed to EqualFacesInCircle
Dim NumberOfSections As Integer = 16
For angle As Double = 0 To 2*MATH.PI Step (2*Math.PI / NumberOfSections)
...rest of your code
Note that doing it this way, the If statement inside your For loop needs to be removed.
Re: How to get Equal Parts or Equal Radians in a Circle ?
OptionBase1
If all you want to do is draw something every n Radians, then the step of your For loop should be n Radians.
I've tried below code and attached the image but not sucessful even for first filled up triangle
Code:
Private newImg As New Bitmap(700, 700)
Private gr As Graphics = Graphics.FromImage(newImg)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cPoint = New Point(newImg.Width / 2, newImg.Width / 2)
Dim radiuss As Integer = newImg.Width / 2
EqualFacesInCircle(gr, cPoint, radiuss)
End Sub
Private Sub EqualFacesInCircle(ByVal gr As Graphics, ByVal centre As PointF, ByVal radius As Single)
Dim bluePen As New Pen(Color.Blue, 2)
Dim NumberOfSections As Integer = 16
For angle As Double = 0 To 2 * Math.PI Step (2 * Math.PI / NumberOfSections)
' outer point of the line
Dim outerX As Single = centre.X + CSng(Math.Cos(angle) * radius)
Dim outerY As Single = centre.Y + CSng(Math.Sin(angle) * radius)
Dim lengthOfTickMark As Integer = 17.5
Dim innerRadius As Integer = radius - lengthOfTickMark
Dim blueBrush As SolidBrush = New SolidBrush(Color.Blue)
Dim lemonAppealBrush As SolidBrush = New SolidBrush(Color.FromArgb(239, 227, 175))
gr.DrawLine(bluePen, New PointF(outerX, outerY), New PointF(centre.X, centre.Y))
Dim radians As Double = angle * (Math.PI / 180)
Dim startX As Double = centre.X + radius * Math.Cos(radians)
Dim startY As Double = centre.Y + radius * Math.Sin(radians)
Dim x1 As Double = centre.X + radius * Math.Cos(radians)
Dim y1 As Double = centre.Y + radius * Math.Sin(radians)
Dim point1 As Point = New Point(centre.X, centre.Y)
Dim point2 As Point = New Point(centre.X, y1)
Dim point3 As Point = New Point(x1, centre.Y)
Dim curvePoints1 As Point() = {point1, point2, point3}
gr.FillPolygon(lemonAppealBrush, curvePoints1)
Next
PictureBox1.Image = newImg
End Sub
Re: How to get Equal Parts or Equal Radians in a Circle ?
Changed the below part
Code:
Dim radians As Double = angle * (Math.PI / 180)
Dim startX As Double = centre.X + radius * Math.Cos(radians)
Dim startY As Double = centre.Y + radius * Math.Sin(radians)
radians += 0.275
Dim x1 As Double = centre.X + radius * Math.Cos(radians)
Dim y1 As Double = centre.Y + radius * Math.Sin(radians)
Dim point1 As Point = New Point(centre.X, centre.Y)
Dim point2 As Point = New Point(x1, y1)
Dim point3 As Point = New Point(x1, centre.X)
Dim curvePoints1 As Point() = {point1, point2, point3}
gr.FillPolygon(lemonAppealBrush, curvePoints1)
And it does not cover full area Marked with Red Circle. I suppose it is the area of Right Angle Triangle
How do I cover the full area and alternately 8 triangles
Re: How to get Equal Parts or Equal Radians in a Circle ?
OptionBase1
Using the For loop I suggested, the angle variable is already measured in Radians. So this line of code:
Code:
Dim radians As Double = angle * (Math.PI / 180)
is just going to mess everything up further.
As per your suggestion did the below marked in blue
Code:
Private Sub EqualFacesInCircle(ByVal gr As Graphics, ByVal centre As PointF, ByVal radius As Single)
Dim bluePen As New Pen(Color.Blue, 2)
Dim NumberOfSections As Integer = 16
For angle As Double = 0 To 2 * Math.PI Step (2 * Math.PI / NumberOfSections)
' outer point of the line
Dim outerX As Single = centre.X + CSng(Math.Cos(angle) * radius)
Dim outerY As Single = centre.Y + CSng(Math.Sin(angle) * radius)
Dim lengthOfTickMark As Integer = 17.5
Dim innerRadius As Integer = radius - lengthOfTickMark
Dim blueBrush As SolidBrush = New SolidBrush(Color.Blue)
Dim lemonAppealBrush As SolidBrush = New SolidBrush(Color.FromArgb(239, 227, 175))
gr.DrawLine(bluePen, New PointF(outerX, outerY), New PointF(centre.X, centre.Y))
Dim startX As Double = centre.X + radius * Math.Cos(angle)
Dim startY As Double = centre.Y + radius * Math.Sin(angle)
angle += 0.275
Dim x1 As Double = centre.X + radius * Math.Cos(angle)
Dim y1 As Double = centre.Y + radius * Math.Sin(angle)
Dim point1 As Point = New Point(centre.X, centre.Y)
Dim point2 As Point = New Point(x1, y1)
Dim point3 As Point = New Point(x1, centre.X)
Dim curvePoints1 As Point() = {point1, point2, point3}
gr.FillPolygon(lemonAppealBrush, curvePoints1)
Next
PictureBox1.Image = newImg
End Sub
Re: How to get Equal Parts or Equal Radians in a Circle ?
I don't know whether I've chosen the right thinking towards creating an array of Points
The reason to create an array of points because if you see below
Code:
....
Dim point1 As Point = New Point(centre.X, centre.Y) 'Which is going to remain common through out
Dim point2 As Point = New Point(x1, y1) '---> Here both X1 and Y1 will change as per the Angle/Radians
Dim point3 As Point = New Point(x1, centre.X) '---> Here Centre.X is going to remain common but X1 point will change
Dim curvePoints1 As Point() = {point1, point2, point3} '----> Whether this syntax needs to be changed ?
gr.FillPolygon(lemonAppealBrush, curvePoints1)
....
and if you see image in #6 it just shows 1 triangle. So for remaining filling(color) in Alternate Triangles thought of creating array of points
As I am coming across for the first time.
So how could i create an Array of Points for Point2 for X1 and Y1 as for this issue I am facing for 1st time.
It is really confusing me to define the Point2 with creation of array of Points with proper syntax reference
Re: How to get Equal Parts or Equal Radians in a Circle ?
OptionBase1
Using the For loop I suggested, the angle variable is already measured in Radians. So this line of code:
Code:
Dim radians As Double = angle * (Math.PI / 180)
is just going to mess everything up further.
You are right
I Have completely modified the code below with addition of function for above mentioned corrections marked in Red.
and managed to get 1 Triangle filled up (Not fully) with color. Img Attached.
Code:
Private newImg As New Bitmap(700, 700)
Private gr As Graphics = Graphics.FromImage(newImg)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cPoint = New Point(newImg.Width / 2, newImg.Width / 2)
Dim radiuss As Integer = newImg.Width / 2
EqualFacesInCircle(gr, cPoint, radiuss)
End Sub
Private Sub EqualFacesInCircle(ByVal gr As Graphics, ByVal centre As PointF, ByVal radius As Single)
'Get different colurs of Pen
Dim bluePen As New Pen(Color.Blue, 2)
Dim NumberOfSections As Integer = 16
For angle As Double = 0 To 2 * Math.PI Step (2 * Math.PI / NumberOfSections)
' outer point of the line
Dim outerX As Single = centre.X + CSng(Math.Cos(angle) * radius)
Dim outerY As Single = centre.Y + CSng(Math.Sin(angle) * radius)
' Inner point of the tick mark
Dim lengthOfTickMark As Integer = 17.5
Dim innerRadius As Integer = radius - lengthOfTickMark
Dim blueBrush As SolidBrush = New SolidBrush(Color.Blue)
Dim RedBrush As SolidBrush = New SolidBrush(Color.Red)
Dim GreenBrush As SolidBrush = New SolidBrush(Color.Green)
gr.DrawLine(bluePen, New PointF(outerX, outerY), New PointF(centre.X, centre.Y))
Dim x1 As Double = centre.X + radius * Math.Cos(DegreesToRadiansDbl(angle))
Dim y1 As Double = centre.Y + radius * Math.Sin(DegreesToRadiansDbl(angle))
Dim triangularPoints As New List(Of Point)
triangularPoints.Add(New Point(x1, y1))
Dim lemonAppealBrush As SolidBrush = New SolidBrush(Color.FromArgb(239, 227, 175))
Dim point1 As Point = New Point(centre.X, centre.Y)
Dim point2 As Point = New Point(x1, y1)
Dim point3 As Point = New Point(x1, centre.X)
Dim curvePoints1 As Point() = {point1, point2, point3}
gr.FillPolygon(lemonAppealBrush, curvePoints1)
Next
PictureBox1.Image = newImg
End Sub
Function DegreesToRadiansDbl(ByVal DblAngle As Double) As Double
DegreesToRadiansDbl = DblAngle * (Math.PI / 180)
DegreesToRadiansDbl += 0.0995 * 2.9
End Function
Now help needed to fill up the triangles alternately
nkvb
Re: How to get Equal Parts or Equal Radians in a Circle ?
Hi
Private messages are welcome, but need to keep answers in the thread rather than in messages as that is the purpose of the Forum.
Here is a complete stand alone example that may be close to what I believe you are asking for. If you want to try this out, then do so with a new test project using a blankForm1 and ALL code as below. Everything is in degrees, no radians used. In this version, the drawing will resize as the Form is resized. There is a final outline applied which can be removed by deleting or commenting out the last section in the Paint Event.
Code:
' blank Form1
' I use Dark background on my Forms so
' you would likely need to adjust the
' colour used below
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim rad As Integer = CInt((Math.Min(ClientSize.Width, ClientSize.Height) \ 2) * 0.9)
Dim ctr As New Point(ClientSize.Width \ 2, ClientSize.Height \ 2)
e.Graphics.TranslateTransform(ctr.X, ctr.Y)
Dim rim As New Rectangle(-rad, -rad, rad * 2, rad * 2)
Dim bsh() As Brush = {Brushes.Green, Brushes.PaleGoldenrod}
Dim sweepAngle As Single = 360 / 16
Dim br As Integer = 0
For ang As Single = -90 To 360 - 90 - sweepAngle Step sweepAngle
e.Graphics.FillPie(bsh(br Mod 2), rim, ang, sweepAngle)
e.Graphics.DrawPie(New Pen(Color.Red, 4), rim, ang, sweepAngle)
br += 1
Next
' if wanted, Draw outline around rim
Using p As New Pen(Color.Red, 8)
e.Graphics.DrawEllipse(p, rim)
End Using
End Sub
' Update draeing on Form Resize
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
Invalidate()
End Sub
End Class
Last edited by FordPrefect; Jan 29th, 2025 at 08:12 AM.
Re: How to get Equal Parts or Equal Radians in a Circle ?
Dear FordPrefect
Private messages are welcome, but need to keep answers in the thread rather than in messages as that is the purpose of the Forum
Agree Dear, Henceforth will take Care and hereby I confess that PM was also sent to OptionBase1. As both of you had replied.
Infact I was trying to understand New List(Of List(Of Point)) ..... still difficult for me to grasp me
and trying to work on as follows
Code:
......
Dim triangularPointsList As New List(Of Point)
myTriangularPointsArray.Add(triangularPointsList)
triangularPointsList.Add(New Point(centre.X, centre.Y))
triangularPointsList.Add(New Point(x1, y1))
triangularPointsList.Add(New Point(x1, centre.X))
gr.FillPolygon(lemonAppealBrush, triangularPointsList.ToArray).....
.......
Not yet completed. But could again lead to very shaby result as I was trying to re-syntax x1 and y1
and in mean time you came up with mind blowing Solution. Thank you so much. This makes me move on
Last Question
If I want Either Green Color Or wanting only PaleGoldenrod as per their respective positions with your code
then What changes do we do in the code ?
Re: How to get Equal Parts or Equal Radians in a Circle ?
Originally Posted by nkvb
If I want Either Green Color Or wanting only PaleGoldenrod as per their respective positions with your code
then What changes do we do in the code ?
nkvb
Hi
If you want to swap the colours then change
Code:
' to swap colours, change this line
Dim bsh() As Brush = {Brushes.Green, Brushes.PaleGoldenrod}
' to this line (not rocket science)
Dim bsh() As Brush = {Brushes.PaleGoldenrod, Brushes.Green}
The New List(Of List(Of Point)) is a List of Objects where each Object is a List(Of Point). Taking an Object by Index would give a complete List(Of Point).
Last edited by FordPrefect; Jan 29th, 2025 at 09:44 AM.
Re: How to get Equal Parts or Equal Radians in a Circle ?
Hi
Really, you should take the initiative to figure out those questions yourself as the details are all there.
Needed to step through a complete circle in increments based on number of segments needed. In this case, 16 parts. So, each segment angle is 360/16 = 22.5 Now, since we are sweeping the arcs, we don't need the final one as its 'space' has been swept by the penultimate segment.
Right. So we now need to single step through 360 by 22.5, BUT, since it might be needed that we start the first segment in the '12 o'clock sweep 22.5 deg' position, so since the default is that 0 deg is on the x axis we need to go negative for starting position which is -90 deg from horizontal ..... hence the starting point is-90 deg. and the ending point is 360 - 90 - 22.5 and step size 22.5. All of that is much easier to understand in the code line than it is to write down.
I do not understand your second question. Here is some code to show the selection of colours from an array of colours, starting at a selected index, stepping through then looping back. I have NO IDEA of any use for this, but here it is.
Code:
Dim bsh() As Brush = {Brushes.PaleGoldenrod, Brushes.Green, Brushes.Red, Brushes.Blue, Brushes.Yellow}
Dim lst As New List(Of Brush)
Dim stepsize As Integer = 2
Dim inx As Integer = 0
For i As Integer = 0 To 111
If i Mod stepsize = 0 Then
lst.Add(bsh(inx))
End If
inx += 1
If inx >= bsh.Length Then inx = 0
Next