Message 1 of 10
Drawing Polygons: Octagon, Hexagon, etc
Not applicable
02-07-2007
11:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
So I need to write a code that can draw an octagon and/or hexagon .. I'd imagine getting the code right for one would be able to be manipulated to draw the other
Researching through these boards, I found this bit of code from Josh ... but I'm having unfortunate results ... is this still the best accepted way of drawing a polygon? Please advise!
User will give the width (height = width), I can specify the number of legs ... just unsure of which route to go or how to go about tackling this monster!
Thank you much!
Ryne
... here is josh's suggestion for drawing polygons that hasn't worked, if this is still the best way of drawing a polygon, let me know and i will try further to manipulate this code for my use - thanks!!!
Here's a snip of how to use it:
Set Hex = Blk.AddLightWeightPolyline(CalculatePolygon(InstPt, 6, 1,
HexDia / 2, 90))
And the function is below. Watch for wordwrap.
-Josh
'creates the verteces list (an array of doubles representing two-element
points) for a defined polygon
'provides the ability to create the polygon in a block or paperspace as
well as modelspace
Function CalculatePolygon(Inst As Variant, Num As Integer, Circ As
Integer, Rad As Double, Optional Ang As Double) As Variant
'where Inst is the insertion point,
'Num is the number of sides,
'Circ defines whether the polygon is inscribed or circumscribed
'Rad is the Radius of the circle,
'and Ang is the optional rotation angle, default will be 0#
Dim PP As Variant, I As Integer
Dim PP2 As Variant, Vertece As Variant
Dim Lin As AcadLine
Dim Base As Double, Hypotenuse As Double
Dim VerteceCount As Integer
VerteceCount = Num * 2 - 1
Dim Vray() As Double
ReDim Vray(0 To VerteceCount) As Double
Dim Degs As Double, ConAng As Double
Degs = 360 / Num
Dim Poly As AcadLWPolyline
PP = ThisDrawing.Utility.PolarPoint(Inst, Radians(Ang), Rad)
If Circ = acInscribed Then
Vray(0) = PP(0): Vray(1) = PP(1)
For I = 1 To (Num - 1)
Ang = Ang + Degs
Vertece = ThisDrawing.Utility.PolarPoint(Inst, Radians(Ang), Rad)
Vray(I * 2) = Vertece(0): Vray(I * 2 + 1) = Vertece(1)
Next I
ElseIf Circ = acCircumscribed Then
ConAng = 360 / (Num * 2)
For I = 1 To Num
Ang = Ang + Degs
PP2 = ThisDrawing.Utility.PolarPoint(Inst, Radians(Ang), Rad)
Set Lin = ThisDrawing.ModelSpace.AddLine(PP2, PP)
Base = Lin.Length / 2
Hypotenuse = Base / Cos(Radians(ConAng))
Vertece = ThisDrawing.Utility.PolarPoint(PP2, Radians(ConAng) +
Lin.Angle, Hypotenuse)
Lin.Delete
Vray((I * 2) - 2) = Vertece(0): Vray((I * 2) - 1) = Vertece(1)
PP(0) = PP2(0): PP(1) = PP2(1)
Next I
End If
CalculatePolygon = Vray
End Function
Researching through these boards, I found this bit of code from Josh ... but I'm having unfortunate results ... is this still the best accepted way of drawing a polygon? Please advise!
User will give the width (height = width), I can specify the number of legs ... just unsure of which route to go or how to go about tackling this monster!
Thank you much!
Ryne
... here is josh's suggestion for drawing polygons that hasn't worked, if this is still the best way of drawing a polygon, let me know and i will try further to manipulate this code for my use - thanks!!!
Here's a snip of how to use it:
Set Hex = Blk.AddLightWeightPolyline(CalculatePolygon(InstPt, 6, 1,
HexDia / 2, 90))
And the function is below. Watch for wordwrap.
-Josh
'creates the verteces list (an array of doubles representing two-element
points) for a defined polygon
'provides the ability to create the polygon in a block or paperspace as
well as modelspace
Function CalculatePolygon(Inst As Variant, Num As Integer, Circ As
Integer, Rad As Double, Optional Ang As Double) As Variant
'where Inst is the insertion point,
'Num is the number of sides,
'Circ defines whether the polygon is inscribed or circumscribed
'Rad is the Radius of the circle,
'and Ang is the optional rotation angle, default will be 0#
Dim PP As Variant, I As Integer
Dim PP2 As Variant, Vertece As Variant
Dim Lin As AcadLine
Dim Base As Double, Hypotenuse As Double
Dim VerteceCount As Integer
VerteceCount = Num * 2 - 1
Dim Vray() As Double
ReDim Vray(0 To VerteceCount) As Double
Dim Degs As Double, ConAng As Double
Degs = 360 / Num
Dim Poly As AcadLWPolyline
PP = ThisDrawing.Utility.PolarPoint(Inst, Radians(Ang), Rad)
If Circ = acInscribed Then
Vray(0) = PP(0): Vray(1) = PP(1)
For I = 1 To (Num - 1)
Ang = Ang + Degs
Vertece = ThisDrawing.Utility.PolarPoint(Inst, Radians(Ang), Rad)
Vray(I * 2) = Vertece(0): Vray(I * 2 + 1) = Vertece(1)
Next I
ElseIf Circ = acCircumscribed Then
ConAng = 360 / (Num * 2)
For I = 1 To Num
Ang = Ang + Degs
PP2 = ThisDrawing.Utility.PolarPoint(Inst, Radians(Ang), Rad)
Set Lin = ThisDrawing.ModelSpace.AddLine(PP2, PP)
Base = Lin.Length / 2
Hypotenuse = Base / Cos(Radians(ConAng))
Vertece = ThisDrawing.Utility.PolarPoint(PP2, Radians(ConAng) +
Lin.Angle, Hypotenuse)
Lin.Delete
Vray((I * 2) - 2) = Vertece(0): Vray((I * 2) - 1) = Vertece(1)
PP(0) = PP2(0): PP(1) = PP2(1)
Next I
End If
CalculatePolygon = Vray
End Function