Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I plot a circle? 8

Status
Not open for further replies.

gecko3

Electrical
Dec 1, 2005
3
I need to make a smith chart application on excel, I have the equations which give me the circles and curves of the shart but I dont know how to put them into excel... can anyone tell me how I can plot a simple circle using the equation for a circle:

(x-a)^2 + (y-b)^2 = r^2

thank you
 
Replies continue below

Recommended for you

There are a lot of ways to do it.

One way would be to parameterize it in terms of theta.

Create a column theta with values from 0 to 2*Pi in small increments.

Create column x calculated as x=a+r*cos(theta)
Create column y calculated as y=b+r*sin(theta)

Do x-y point plot using the x and y columns.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
You can also just "draw" a circle in Excel using the shapes tool on the drawing menu.
 
If you used a drawing object, it would change position/dimensions (relative to a,b,r) every time the plot rescaled.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
electricpete,

Did you mean to say that the problem with using a drawing object is that it would NOT change each time a, b or r changed ?

dumbcivilruss
 
Well the drawing the object wouldnt work cause I need my equation so I can fit it with different values.... I actually need to draw several circles of different dimensions all on the same plot... I'm trying to make a smith chart.
 
Yes, it stays in the same position on the screen which is a change relative to the new chart.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
To plot a circle on an x-y chart, simply solve the circle equation for x, and then plot x,y and -x,y. The more difficult problem with plotting a circle is getting both the x and y axes to the same scale so the plotted circle is actually circular. I posted that question a number of years ago and someone (I forget who) helped me with a VB macro, which I then modified somewhat. Here it is:

Code:
Attribute VB_Name = "SquareGrid"
Option Explicit

Sub MakePlotGridSquare()
    
    Dim plotInHt As Integer, plotInWd As Integer
        
    Dim Ymax As Double, Ymin As Double, Ydel As Double
    Dim Xmax As Double, Xmin As Double, Xdel As Double
    Dim Ypix As Double, Xpix As Double, GridSz As Double
    
    With ActiveChart
        ' get plot size
        With .PlotArea
            plotInHt = .InsideHeight
            plotInWd = .InsideWidth
        End With
        
        ' get axis dimensions and lock scales
        With .Axes(xlValue)
            Ymax = .MaximumScale
            Ymin = .MinimumScale
            Ydel = .MajorUnit
            .MaximumScaleIsAuto = False
            .MinimumScaleIsAuto = False
            .MajorUnitIsAuto = False
        End With
        With .Axes(xlCategory)
            Xmax = .MaximumScale
            Xmin = .MinimumScale
            Xdel = .MajorUnit
            .MaximumScaleIsAuto = False
            .MinimumScaleIsAuto = False
            .MajorUnitIsAuto = False
        End With
        
        ' determine grid size to utilize
        If Ydel >= Xdel Then
            GridSz = Ydel
        Else
            GridSz = Xdel
        End If
        
        .Axes(xlValue).MajorUnit = GridSz
        .Axes(xlCategory).MajorUnit = GridSz
        
        ' pixels per grid
        Ypix = plotInHt * GridSz / (Ymax - Ymin)
        Xpix = plotInWd * GridSz / (Xmax - Xmin)
        
        ' Keep plot size as is, adjust max scales
        If Xpix > Ypix Then
            .Axes(xlCategory).MaximumScale = plotInWd * GridSz / Ypix + Xmin
        Else
            .Axes(xlValue).MaximumScale = plotInHt * GridSz / Xpix + Ymin
        End If
    End With

End Sub
 
I vote a star to you Panars. That is a feature I need for plotting vectors. One downside of the function as written is that it turns off auto-scaling. If you try to use the function a second time on different data, it doesn't exactly work. Some of your data might be outsie the range for example.

I thought it would be useful to add code at the beginning of the function that let excel auto-update the graph scales before running the rest of the function. Now it works great. One macro and the spreadsheet is auto-scaled with 1:1 aspect ratio. Here is the modified code:

Sub MakePlotGridSquare2()

Dim plotInHt As Integer, plotInWd As Integer

Dim Ymax As Double, Ymin As Double, Ydel As Double
Dim Xmax As Double, Xmin As Double, Xdel As Double
Dim Ypix As Double, Xpix As Double, GridSz As Double
' my part below set auto first
With ActiveChart
With .Axes(xlValue)
.MaximumScaleIsAuto = True
.MinimumScaleIsAuto = True
.MajorUnitIsAuto = True
End With
With .Axes(xlCategory)
.MaximumScaleIsAuto = True
.MinimumScaleIsAuto = True
.MajorUnitIsAuto = True
End With
End With
' now his part
With ActiveChart
' get plot size
With .PlotArea
plotInHt = .InsideHeight
plotInWd = .InsideWidth
End With

' get axis dimensions and lock scales
With .Axes(xlValue)
Ymax = .MaximumScale
Ymin = .MinimumScale
Ydel = .MajorUnit
.MaximumScaleIsAuto = False
.MinimumScaleIsAuto = False
.MajorUnitIsAuto = False
End With
With .Axes(xlCategory)
Xmax = .MaximumScale
Xmin = .MinimumScale
Xdel = .MajorUnit
.MaximumScaleIsAuto = False
.MinimumScaleIsAuto = False
.MajorUnitIsAuto = False
End With

' determine grid size to utilize
If Ydel >= Xdel Then
GridSz = Ydel
Else
GridSz = Xdel
End If

.Axes(xlValue).MajorUnit = GridSz
.Axes(xlCategory).MajorUnit = GridSz

' pixels per grid
Ypix = plotInHt * GridSz / (Ymax - Ymin)
Xpix = plotInWd * GridSz / (Xmax - Xmin)

' Keep plot size as is, adjust max scales
If Xpix > Ypix Then
.Axes(xlCategory).MaximumScale = plotInWd * GridSz / Ypix + Xmin
Else
.Axes(xlValue).MaximumScale = plotInHt * GridSz / Xpix + Ymin
End If
End With

End Sub

=============
I do have a question. What is the purpose of the follwoing code?
Attribute VB_Name = "SquareGrid"



=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
Electricpete, I was just about to make an identical comment and suggestion.

Planars, you have given me a bypass to a long-standing irritation.
 
Thanks, although like I said I can't take all the credit.
I was aware of the auto-scale problem, but it was never a big problem with me. Thanks for the revision though.

Regarding the line: Attribute VB_Name = "SquareGrid"
this is included by Excel when you export code from the VB editor to a file. It is the name of the module.
 
I hit another limitation with the above macro last night: it only works if the plot includes both axes. I then spent a few hours googling for a work-around, without success, and will have to see if I can come up with something myself. (One gets so lazy when one has Google at one's fingertips.) If my head breaks through the brick wall I'll post the result here.

In my (unsuccessful) googling, I did find what seems to be the source of Panar's macro. It looks like it was written by a Jon Peltier. See where he presents and describes his approach. It does not, however, incorporate the electricpete improvement.
 
I think I have come up with a solution. See below.
[tt]
----------
Sub MakePlotGridSquare()
'
' Changes the scale of an Excel graph along one of its two axes by exactly
' the right amount to result in equal X and Y scales.
' Note that the chart has to be "active" when the macro is run.
'
' Grabbed from Eng-Tips' "Spreadsheets" forum, where it was placed
' by Panars on 2 Dec 2005. Electricpete then made an improvement in that forum.
'
' Subsequent modifications made to accommodate plots without axes.
'
' Original source appears to have been
' '
' Macro seems to have intermittent difficulties if an axis title
' overlaps with the actual axis, in that it sometimes moves things around
' and sometimes doesn't.
' However no engineer would allow such a graph on a spreadsheet.
'
Dim plotInHt As Integer, plotInWd As Integer
Dim HaveXaxis As Boolean, HaveYaxis As Boolean
Dim Ymax As Double, Ymin As Double, Ydel As Double
Dim Xmax As Double, Xmin As Double, Xdel As Double
Dim Ypix As Double, Xpix As Double, GridSz As Double
'
With ActiveChart
'
' Get plot size.
'
With .PlotArea
plotInHt = .InsideHeight
plotInWd = .InsideWidth
End With
'
' Get presence/absence for each axis.
'
HaveXaxis = .HasAxis(xlCategory)
HaveYaxis = .HasAxis(xlValue)
'
' Deal first with the X axis.
' (1) Turn it on if it is not already on;
' (2) Set its scaling stuff to "auto";
' (3) Record its extreme values and then lock the scale.
'
If Not HaveXaxis Then .HasAxis(xlCategory) = True
With .Axes(xlCategory)
.MaximumScaleIsAuto = True
.MinimumScaleIsAuto = True
.MajorUnitIsAuto = True
End With
With .Axes(xlCategory)
Xmax = .MaximumScale
Xmin = .MinimumScale
Xdel = .MajorUnit
.MaximumScaleIsAuto = False
.MinimumScaleIsAuto = False
.MajorUnitIsAuto = False
End With
'
' Repeat the process for the Y axis.
'
If Not HaveYaxis Then .HasAxis(xlValue) = True
With .Axes(xlValue)
.MaximumScaleIsAuto = True
.MinimumScaleIsAuto = True
.MajorUnitIsAuto = True
End With
With .Axes(xlValue)
Ymax = .MaximumScale
Ymin = .MinimumScale
Ydel = .MajorUnit
.MaximumScaleIsAuto = False
.MinimumScaleIsAuto = False
.MajorUnitIsAuto = False
End With
'
' Determine grid size to utilize.
'
If Ydel >= Xdel Then
GridSz = Ydel
Else
GridSz = Xdel
End If
'
.Axes(xlValue).MajorUnit = GridSz
.Axes(xlCategory).MajorUnit = GridSz
'
' Pixels per grid ...
'
Ypix = plotInHt * GridSz / (Ymax - Ymin)
Xpix = plotInWd * GridSz / (Xmax - Xmin)
'
' Keep plot size as is, but adjust the appropriate scale.
'
If Xpix > Ypix Then
.Axes(xlCategory).MaximumScale = plotInWd * GridSz / Ypix + Xmin
Else
.Axes(xlValue).MaximumScale = plotInHt * GridSz / Xpix + Ymin
End If
'
' Return presence/absence of axes back to the way it was.
'
If Not HaveXaxis Then .HasAxis(xlCategory) = False
If Not HaveYaxis Then .HasAxis(xlValue) = False
End With
'
End Sub
----------
[/tt]
Please attack it with gusto: I want it to end up as bullet-proof as possible.

PS. How do I have my code appear in a snappy little subwindow like Panars used above?
 
Denial-

Yes. It was Jon Peltier who came up with the code I posted. Thanks for the reminder. He responded to my question in microsoft.public.excel newsgroup.

I don't understand why you want to square axes when your chart does not have either an X or Y axis. What are you squaring to?
 
Because my chart is not a graph: it is a diagram. I am using Excel's XY plot capability to produce a diagram from a table of calculated and volatile values.

However this is something I frequently do, and the best I have been able to achieve until now is very approximate scaling equality.

My most recent application, and the one into which I have already incorporated your code, is for calculating the bending moments in an infinite slab resting on the ground and loaded by a set of arbitrarily shaped pressure loads. I want my diagram to show the user the actual shape of the loading pattern he is specifying, and the better the scaling the better the visual check that results.

Incidentally, my slab spreadsheet uses worksheet protection in an attempt to immunise itself against wayward fingers. The macro does not work on a protected sheet. I have temporarily circumvented this by turning protection off, strutting my stuff, then turning protection back on. This is far from ideal, and at some stage I will investigate the possibility of a solution that does not involve embedding a password in VBA code. But at the moment, while I might have slain one dragon other unrelated ones are awakening.
 
Denial-

Thanks for the clarification. I too have used x-y graphs to draw "objects", in my case it was a diagram of a pile group. However, I include the axes to show the scale, so I didn't run into your problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor