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
(x-a)^2 + (y-b)^2 = r^2
thank you





RE: How do I plot a circle?
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?
RE: How do I plot a circle?
=====================================
Eng-tips forums: The best place on the web for engineering discussions.
RE: How do I plot a circle?
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?
RE: How do I plot a circle?
http://
http://
RE: How do I plot a circle?
=====================================
Eng-tips forums: The best place on the web for engineering discussions.
RE: How do I plot a circle?
CODE
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?
http://www.sss-mag.com/txt/SmithChartRevD.xls
RE: How do I plot a circle?
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?
Planars, you have given me a bypass to a long-standing irritation.
RE: How do I plot a circle?
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?
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 h
where he presents and describes his approach. It does not, however, incorporate the electricpete improvement.
RE: How do I plot a circle?
----------
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
' h
'
' 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?
See ht
"code" tag
Hg
Eng-Tips policies: FAQ731-376
RE: How do I plot a circle?
CODE
RE: How do I plot a circle?
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?
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?
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?
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?
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.
htt
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?
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?
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?
=====================================
Eng-tips forums: The best place on the web for engineering discussions.