×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How do I plot a circle?
8

How do I plot a circle?

How do I plot a circle?

(OP)
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

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

You can also just "draw" a circle in Excel using the shapes tool on the drawing menu.

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

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

RE: How do I plot a circle?

(OP)
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.

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

4
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

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

Electricpete, I was just about to make an identical comment and suggestion.

Planars, you have given me a bypass to a long-standing irritation.

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

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 http://peltiertech.com/Excel/Charts/SquareGrid.html
where he presents and describes his approach.  It does not, however, incorporate the electricpete improvement.

RE: How do I plot a circle?

I think I have come up with a solution.  See below.

----------
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
'  http://peltiertech.com/Excel/Charts/SquareGrid.html
'
'  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
----------

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?

RE: How do I plot a circle?

CODE

Thanks.

RE: How do I plot a circle?

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?

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

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.

RE: How do I plot a circle?

I don't know if this helps you in any way, but although the Excel sheet protection is far from ideal, you can customize a little. You can disable the "locked" property for both cells and objects (such as your chart and circle), to allow it to be modified even though the sheet is protected, and you can protect the sheets for contents (= cells), objects (shapes, charts), and scenarios. Maybe this helps you to set it up the way you want (or close to that).

Cheers,
Joerd

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.

RE: How do I plot a circle?

Denial-
I guess you can protect a worksheet from the user but allow it to be modified by macros.  I just found this on John Walkenbach's spreadsheet page.

http://www.j-walk.com/ss/excel/faqs/protectionFAQ.htm

Can I set things up so my VBA macro can make changes to Locked cells on a protected sheet?
Yes, you can write a macro that protects the worksheet, but still allows changes via macro code. The trick is to protect the sheet with the UserInterfaceOnly parameter. Here's an example:

  ActiveSheet.Protect UserInterfaceOnly:=True
After this statement is executed, the worksheet is protected -- but your VBA code will still be able to make changes to locked cells and perform other operation that are not possible on a protected worksheet.

RE: How do I plot a circle?

Panars,

You had me worried there for a moment:  I thought I was going to end up owing you two beers rather than just the one.  Luckily this turned out not to be the case, and I was rescued by a subtle Microsoft gotcha.

My objective is to avoid having to embed the password in the VBA code.  However the state UserInterfaceOnly:=True does not persist when the worksheet is closed and then reopened.  The way around this, of course, is to reset the worksheet to UserInterfaceOnly:=True when the workbook is opened.

And guess what?  Setting UserInterfaceOnly:=True on a locked sheet requires a password (on Excel 2002 at least).  So all I achieved was moving the embedded password from one bit of code to another.

But thanks for the suggestion.  I knew nothing about UserInterfaceOnly, and the knowledge I have gained will be useful somewhere.

In due course I'll investigate Joerd's suggestions (for which, my thanks).

RE: How do I plot a circle?

I found an easy way to keep X and Y scales equal without using VBA.

step 1. Make chart area square.

step 2.

Add an extra graph to the chart. It will have only two points from lower left corner to upper right corner.
Condition Y2-Y1 = X2-X1 will assure equal X and Y scales.

calculate follwing values

dX = Xmax - Xmin
dY = Ymax - Ymin
extraX = max(dY-dX,0)
extraY = max(dX-dY,0)

where Xmax, Xmin, Ymax and Ymin are maximum and minimum values of all graphs in the chart.

coordinates of diagonal line
X1 = Xmin - extraX/2 : Y1 = Ymin - extraY/2
X2 = Xmax + extraX/2 : Y2 = Ymax + extraY/2

to make this line invisible in Format Data Series set Line and Marker to none.









RE: How do I plot a circle?

That's a good way to do it without vba.  Also has the advantage that you don't have to run the vba macro for scaling.  I'm happy with the vba solution - no clutter on the spreadsheet itself and can plug it into another spreadsheet with a single cut/paste.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources