Skip to content

Instantly share code, notes, and snippets.

@juaxix
Created March 14, 2013 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juaxix/5160693 to your computer and use it in GitHub Desktop.
Save juaxix/5160693 to your computer and use it in GitHub Desktop.
3D Electric Fields - Real Basic code -- WIP
Sub Click(X as integer, Y as integer)
//Clicking on an object on screen selects it in the listbox as well as on screen
dim Obj As PD3DObject
dim ChrgObj As ChargedObject
dim index,i As integer
Obj = PD3DObject(Me.PickedObj(X,Y)) //get the object clicked on
if (Obj <> nil) then
if Obj isA ChargeSphere then //the user has selected a charge config
ChrgObj = ChargeSphere(Obj).parent //trace back from the individual charge to its ChrgObj
for i=0 to ubound(ChrgObjList)
if ChrgObj = ChrgObjList(i) then
index = i //find the index in the ChrgObjList
i = MsgBox ("Carga de este punto:"+Str(ChrgObj.ChargeList(i).Charge)+"Newton/Culombio",64,"Valor de la carga")
end if
next
SelectedObject = ChrgObj //update our selected obj pointer
//Update the selection in the listbox
for i = 0 to Ubound(ChrgObjList)
if i = index then ObjectList.Selected(i) = True else ObjectList.Selected(i) = False
next
elseif Obj isA cilindroCampo then
i = MsgBox ("Campo en este punto:"+Str(cilindroCampo(Obj).campo)+" Newton/Culombio",64,"Medida del Campo")
elseif Obj isA conoCampo then
i = MsgBox ("Campo en este punto:"+Str(conoCampo(Obj).campo)+"Newton/Culombio",64,"Medida del Campo")
end if
end if
//update the onscreen highlight of charged abjects
UpdateHighlight
End
Protected Sub CalcEField()
//Este metodo calcula y muestra los campos electricos como miembro de la ventana principal
dim E, fpos as vector3D
dim i, j, k, qi, n, m, l as integer
dim pfactor as double
dim campo as double
dim campo_total As Double
dim EArrow as PDRoundArrow
Redim Matrix(-1)
dim supress as boolean //La bandera supress se activa para calcular el campo solo si se esta lo suficientemente cerca del punto
//o si el objeto en cuestion es una esfera y el usuario ha elegido no pintar el campo externo o interno.
pfactor = 50
MyPDSpace.RemoveAllObjects // Para quitar todos los Objects
n = UBound(ChrgObjList)
//Mostramos las cargas
for qi = 0 to n
if ChrgObjList(qi) = SelectedObject then
ChrgObjList(qi).DisplaySelected
else
ChrgObjList(qi).DisplayIt
end if
next
//Calcula y muestra los vectores del campo electrico E
fpos = New Vector3D(0,0,0)
for i = 0 to 2*MatrixSize
for j = 0 to 2*MatrixSize
for k = 0 to 2*MatrixSize
fpos.X = (i-MatrixSize)*pfactor
fpos.Y = (j-MatrixSize)*pfactor
fpos.Z = (k-MatrixSize)*pfactor
E = New Vector3D(0,0,0)
campo_total = 0.0
for qi = 0 to n //Vamos sumando las cargas de los objetos
E.Add(ChrgObjList(qi).EFieldAt(fpos,campo))
campo_total = campo + campo_total
next
m = uBound(SurfaceList)
for l = 0 to m
if SurfaceList(l).DistCheck(fpos) then
supress = true
end if
if SurfaceList(l) isA SphereData then
select case SphereData(SurfaceList(l)).ShowVectors
case 0
case 1
if DistForm(SphereData(SurfaceList(l)).Center,fpos) > SphereData(SurfaceList(l)).radius then supress = true
case 2
if DistForm(SphereData(SurfaceList(l)).Center,fpos) < SphereData(SurfaceList(l)).radius then supress = true
end select
end if
next
E.MultiplyBy(EScale) //Es el valor de la escala del deslizador de la interfaz
if E.Length > 5 and E.Length < 500 then
//Si el campo total esta fuera del rango en un punto dado no se dibuja
if supress = false then
EArrow = New PDRoundArrow(fpos, PDYellow, E,campo_total)
end if
end if
//Si el punto no se ha marcado para calcular ,no cuenta como dentro del rango, la caja de la matriz para calcular E
if supress = false then
if MatrixOn then
Matrix.Append New PDBox(fpos,&c00ff00,new Vector3D(3,3,3),new Vector3D(3,0,0),new Vector3D(0,10,0))
end if
end if
supress = false
next
next
next
//EJES
m = uBound(SurfaceList)
for l = 0 to m
if SurfaceList(l).gridEnabled then SurfaceList(l).DrawGrid
next
//Actualiza la seleccion del objeto listbox para establecer como seleccionado el objeto actual
for i=0 to ubound(ChrgObjList)
if SelectedObject = ChrgObjList(i) then ObjectList.Selected(i) = true else ObjectList.Selected(i) = false
next
//muestra las pantallas de ayuda
for i=0 to ubound(ScreenList)
ScreenList(i).Display
next
//cajas
DemoBox.v1 = DemoBoxCenter
DemoBox.Display(DemoBoxSize)
PD3D.SelectedSpace.Update
End
Protected Sub CrearDisco()
dim q, x, y, z, L, R as double
dim n as integer
dim grid, positive,uniforme as boolean
dim theCenter,Normal as Vector3D
dim AxisNum as integer
dim myData as DatosDisco
dim myObj as DiscoCargado
q = CDbl(PlantillaDisco.EF(3).text)
x = CDbl(PlantillaDisco.EF(0).text)
y = CDbl(PlantillaDisco.EF(1).text)
z = CDbl(PlantillaDisco.EF(2).text)
n = CDbl(PlantillaDisco.EF(5).text)
dim num_cargas as integer = Val(PlantillaDisco.EF(7).text)
if num_cargas <=0 or num_cargas>100 then num_cargas=12
//L = CDbl(PlantillaDisco.EF(4).text)
//La longitud de un disco siempre sera 1
L = 1
R = CDbl(PlantillaDisco.EF(6).text)
grid = PlantillaDisco.CBGrid.value
uniforme = PlantillaDisco.CBCargado.Value
if L = 0 then L = 1000
if n < 1 then n = 1
if q = 0 then q = 1
if q>0 then
positive = true
else
positive = false
end if
theCenter = New Vector3D(x,y,z)
AxisNum = PlantillaDisco.AxisMenu.ListIndex
select case AxisNum
case 0
Normal = new Vector3D(1,0,0)
case 1
Normal = new Vector3D(0,1,0)
case 2
Normal = new Vector3D(0,0,1)
end select
myData = new DatosDisco(theCenter,Normal,R,L,n,grid,positive)
SurfaceList.Append myData
myObj = New DiscoCargado(q, n, theCenter, Normal,L,R,uniforme,CDbl(PlantillaDisco.EF(8).Text), num_cargas)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
myObj.ImportData
SelectedObject = myObj
ChrgObjList.Append myObj
CalcEField
End
Protected Sub MakeCylinder()
dim q, x, y, z, L, R as double
dim n as integer
dim grid, positive as boolean
dim theCenter,Normal as Vector3D
dim AxisNum as integer
dim myData as CylinderData
dim myObj as CylinderCharge
q = CDbl(CylinderSheet.EF(3).text)
x = CDbl(CylinderSheet.EF(0).text)
y = CDbl(CylinderSheet.EF(1).text)
z = CDbl(CylinderSheet.EF(2).text)
n = CDbl(CylinderSheet.EF(5).text)
L = CDbl(CylinderSheet.EF(4).text)
R = CDbl(CylinderSheet.EF(6).text)
grid = CylinderSheet.CBGrid.value
if L = 0 then L = 1000
if n < 1 then n = 1
if q = 0 then q = 1
if q>0 then
positive = true
else
positive = false
end if
theCenter = New Vector3D(x,y,z)
AxisNum = CylinderSheet.AxisMenu.ListIndex
select case AxisNum
case 0
Normal = new Vector3D(1,0,0)
case 1
Normal = new Vector3D(0,1,0)
case 2
Normal = new Vector3D(0,0,1)
end select
myData = new CylinderData(theCenter,Normal,R,L,n,grid,positive)
SurfaceList.Append myData
myObj = New CylinderCharge(q, n, theCenter, Normal,L,R)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
myObj.ImportData
SelectedObject = myObj
ChrgObjList.Append myObj
CalcEField
End
Protected Sub MakeLine()
dim q, x, y, z, vx, vy, vz, L as double
dim n as integer
dim grid as boolean
dim theLine as LineCharge
dim myData as LineData
dim theAxis, Position as Vector3D
dim positive as boolean
dim myObj As LineCharge
q = CDbl(LineSheet.EF(7).text)
x = CDbl(LineSheet.EF(1).text)
y = CDbl(LineSheet.EF(2).text)
z = CDbl(LineSheet.EF(3).text)
vx = CDbl(LineSheet.EF(4).text)
vy = CDbl(LineSheet.EF(5).text)
vz = CDbl(LineSheet.EF(6).text)
n = CDbl(LineSheet.EF(9).text)
L = CDbl(LineSheet.EF(8).text)
grid = LineSheet.CBGrid.value
if L = 0 then L = 1000
if n < 1 then n = 1
if q = 0 then q = 1
if q>0 then
positive = true
else
positive = false
end if
Position = New Vector3D(x,y,z)
theAxis = new Vector3D(vx,vy,vz)
theAxis.normalize
theAxis.MultiplyBy(L)
myData = new LineData(Position,theAxis,grid,positive)
SurfaceList.Append myData
myObj = New LineCharge(q, n, Position, theAxis)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
myObj.ImportData
SelectedObject = myObj
ChrgObjList.Append myObj
CalcEField
End
Protected Sub MakePlane()
dim q, x, y, z, L as double
dim n as integer
dim grid,positive as boolean
dim thePlane as PlaneCharge
dim theCenter as Vector3D
dim planeNum as integer
dim myData as PlaneData
dim myObj as PlaneCharge
q = CDbl(PlaneSheet.EF(4).text)
x = CDbl(PlaneSheet.EF(1).text)
y = CDbl(PlaneSheet.EF(2).text)
z = CDbl(PlaneSheet.EF(3).text)
n = CDbl(PlaneSheet.EF(0).text)
L = CDbl(PlaneSheet.EF(5).text)
grid = PlaneSheet.CBGrid.value
if L = 0 then L = 1000
if n < 1 then n = 1
if q = 0 then q = 1
if q>0 then
positive = true
else
positive = false
end if
theCenter = New Vector3D(x,y,z)
planeNum = PlaneSheet.PUAxis.ListIndex
select case planeNum
case 0
myData = new PlaneData(0,0,1,x,y,z,L,n,grid,positive)
case 1
myData = new PlaneData(0,1,0,x,y,z,L,n,grid,positive)
case 2
myData = new PlaneData(1,0,0,x,y,z,L,n,grid,positive)
end select
SurfaceList.Append myData
myObj = New PlaneCharge(q, n, theCenter, L,planeNum)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
myObj.ImportData
SelectedObject = myObj
ChrgObjList.Append myObj
CalcEField
End
Protected Sub MakePoint()
dim q, x, y, z as double
dim myObj as SingleCharge
q = CDbl(SingleSheet.EF(3).text)
x = CDbl(SingleSheet.EF(2).text)
y = CDbl(SingleSheet.EF(1).text)
z = CDbl(SingleSheet.EF(0).text)
myObj = New SingleCharge(q, x, y, z)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
myObj.ImportData
SurfaceList.Append new SphereData(new Vector3D(x,y,z),2)
ChrgObjList.Append myObj
SelectedObject = myObj
CalcEField
End
Protected Sub MakeRing()
dim q, x, y, z, nx, ny, nz, R as double
dim n as integer
dim grid, positive as boolean
dim theRing as RingCharge
dim theCenter, Norman as Vector3D
dim myData as RingData
dim myObj as RingCharge
q = CDbl(RingSheet.EF(6).text)
x = CDbl(RingSheet.EF(0).text)
y = CDbl(RingSheet.EF(1).text)
z = CDbl(RingSheet.EF(2).text)
R = CDbl(RingSheet.EF(7).text)
nx = CDbl(RingSheet.EF(3).text)
ny = CDbl(RingSheet.EF(4).text)
nz = CDbl(RingSheet.EF(5).text)
n = CDbl(RingSheet.EF(8).text)
grid = RingSheet.CBGrid.value
if n < 4 then n = 4
if R = 0 then R = 300
if q = 0 then q = 1
if q>0 then
positive = true
else
positive = false
end if
if nx = 0 and ny = 0 and nz = 0 then
Norman = new Vector3D(1,0,0)
else
Norman = new Vector3D(nx,ny,nz)
end if
theCenter = New Vector3D(x,y,z)
myData = new RingData(theCenter,R,Norman,n,grid,positive)
SurfaceList.Append myData
myObj = New RingCharge(q, theCenter, R, Norman, n)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
myObj.ImportData
ChrgObjList.Append myObj
SelectedObject = myObj
CalcEField
End
Protected Sub MakeScreen()
dim x, y, z, num as double
dim theCenter,Normal,up as Vector3D
dim myObj as Scree
x = CDbl(ScreenSheet.EF(0).text)
y = CDbl(ScreenSheet.EF(1).text)
z = CDbl(ScreenSheet.EF(2).text)
num = ScreenSheet.PuAxis.ListIndex
select case num
case 0//PLANO xy
Normal = new Vector3D(0,1,0)
theCenter = new Vector3D(0,0,z)
up = new Vector3D(0,0,1)
case 1//xz
Normal = new Vector3D(0,0,1)
theCenter = new Vector3D(0,y,0)
up = new Vector3D(0,1,0)
case 2//yz
Normal = new Vector3D(0,0,1)
theCenter = new Vector3D(x,0,0)
up = new Vector3D(1,0,0)
end select
ScreenList.Append new Scree(theCenter,Normal,up)
CalcEField
End
Protected Sub MakeSphere()
dim q, x, y, z, R as double
dim theSphere as SphereCharge
dim theCenter as Vector3D
dim myData as SphereData
dim grid, positive as boolean
dim myObj as SphereCharge
dim outside,inside,both As Boolean
q = CDbl(SphereSheet.EF(3).text)
x = CDbl(SphereSheet.EF(0).text)
y = CDbl(SphereSheet.EF(1).text)
z = CDbl(SphereSheet.EF(2).text)
R = CDbl(SphereSheet.EF(4).text)
outside = SphereSheet.Outside.value
inside = SphereSheet.Inside.value
both = SphereSheet.Both.value
grid = SphereSheet.CBGrid.value
if R = 0 then R = 300
if q = 0 then q = 1
if q>0 then
positive = true
else
positive = false
end if
theCenter = New Vector3D(x,y,z)
if outside then myData = new SphereData(theCenter,R,grid,positive,2)
if inside then myData = new SphereData(theCenter,R,grid,positive,1)
if both then myData = new SphereData(theCenter,R,grid,positive,0)
SurfaceList.Append myData
myObj = New SphereCharge(q, theCenter, R)
ObjectList.InsertRow(uBound(ChrgObjList)+1,myObj.name)
SelectedObject = myObj
myObj.ImportData
ChrgObjList.Append myObj
CalcEField
End
Protected Sub RemoveChrgObject(ChrObj As ChargedObject)
dim i as integer
for i=0 to ubound(ChrObj.ChargeList)
PD3D.SelectedSpace.RemoveObject ChrObj.ChargeList(i).GraphRep
next
End
Protected Sub UpdateDemoBox()
dim i As integer
if ubound(DemoBox.GraphRep) > 2 then
for i=0 to ubound(DemoBox.GraphRep)
PD3D.SelectedSpace.RemoveObject DemoBox.GraphRep(i)
next
end if
DemoBox.v1 = DemoBoxCenter
DemoBox.Display(DemoBoxSize)
PD3D.SelectedSpace.Update
End
Protected Sub UpdateHighlight()
//El objeto seleccionado es muestra mas oscuro que el resto
dim i,j as integer
for j=0 to ubound(ChrgObjList)
if ChrgObjList(j) = SelectedObject then
RemoveChrgObject(ChrgObjList(j))
ChrgObjList(j).DisplaySelected
else
if ChrgObjList(j).ChargeList(0).GraphRep.myColor = PDDarkRed or _
ChrgObjList(j).ChargeList(0).GraphRep.myColor = PDDarkBlue then
RemoveChrgObject(ChrgObjList(j))
ChrgObjList(j).DisplayIt
end if
end if
next
PD3D.SelectedSpace.Update
End
Protected Sub UpdateMatrix()
//Repinta la matriz de puntos mismo proceso que CalcEfield
dim i,j,k,m,l As integer
dim fpos as Vector3D
dim supress as boolean
if not MatrixOn then
for i=0 to ubound(Matrix)
PD3D.SelectedSpace.RemoveObject Matrix(i)
next
end if
Redim Matrix(-1)
dim pfactor as double
fpos = new Vector3D(0,0,0)
pfactor = 50
if MatrixOn then
for i = 0 to 2*MatrixSize
for j = 0 to 2*MatrixSize
for k = 0 to 2*MatrixSize
fpos.X = (i-MatrixSize)*pfactor
fpos.Y = (j-MatrixSize)*pfactor
fpos.Z = (k-MatrixSize)*pfactor
m = uBound(SurfaceList)
for l = 0 to m
if SurfaceList(l).DistCheck(fpos) then
supress = true
end if
if SurfaceList(l) isA SphereData then
select case SphereData(SurfaceList(l)).ShowVectors
case 0
case 1
if DistForm(SphereData(SurfaceList(l)).Center,fpos) > SphereData(SurfaceList(l)).radius then supress = true
case 2
if DistForm(SphereData(SurfaceList(l)).Center,fpos) < SphereData(SurfaceList(l)).radius then supress = true
end select
end if
next
if not supress then
Matrix.Append New PDBox(fpos,&c00ff00,new Vector3D(3,3,3),new Vector3D(3,0,0),new Vector3D(0,10,0))
end if
supress = false
next
next
next
end if
PD3D.SelectedSpace.Update
End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment