Buchempfehlung
Visual Basic 6 Kochbuch
Visual Basic 6 Kochbuch
Viele praktische Tipps zum Programmieren mit Visual Basic 6, die sich oft auch auf FB übertragen lassen. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

Rechnen mit komplexen Zahlen

Uploader:Redakteurnemored
Datum/Zeit:12.09.2007 15:27:17

type complex
  as double real, imag, abs, arg
end type

declare function newCartesian(byval real as double, byval imag as double) as complex
declare function newPolar(byval abs as double, byval arg as double) as complex
declare function complexAdd(byval c1 as complex, byval c2 as complex) as complex
declare function complexSub(byval c1 as complex, byval c2 as complex) as complex
declare function complexMult(byval c1 as complex, byval c2 as complex) as complex
declare function complexDiv(byval c1 as complex, byval c2 as complex) as complex
declare sub polar2cartesian(byref c as complex)
declare sub cartesian2polar(byref c as complex)

function newCartesian(byval real as double, byval imag as double) as complex
  ' neue komplexe Zahl anhand von Real- und Imaginaerteil definieren
  dim as complex ret
  ret.real = real
  ret.imag = imag
  cartesian2polar(ret)
  return ret
end function

function newPolar(byval r as double, byval phi as double) as complex
  ' neue komplexe Zahl anhand der Polarkoordinaten definieren
  dim as complex ret
  ret.abs = r
  ret.arg = phi
  polar2cartesian(ret)
  return ret
end function

function complexAdd(byval c1 as complex, byval c2 as complex) as complex
  ' addiert zwei komplexe Zahlen
  dim as complex ret
  ret.real = c1.real + c2.real
  ret.imag = c1.imag + c2.imag
  cartesian2polar(ret)
  return ret
end function

function complexSub(byval c1 as complex, byval c2 as complex) as complex
  ' subtrahiert zwei komplexe Zahlen
  dim as complex ret
  ret.real = c1.real - c2.real
  ret.imag = c1.imag - c2.imag
  cartesian2polar(ret)
  return ret
end function

function complexMult(byval c1 as complex, byval c2 as complex) as complex
  ' multipliziert zwei komplexe Zahlen
  dim as complex ret
  ret.abs = c1.abs * c2.abs
  ret.arg = c1.arg + c2.arg
  polar2cartesian(ret)
  return ret
end function

function complexDiv(byval c1 as complex, byval c2 as complex) as complex
  ' dividiert zwei komplexe Zahlen
  dim as complex ret
  ret.abs = c1.abs / c2.abs
  ret.arg = c1.arg - c2.arg
  polar2cartesian(ret)
  return ret
end function

sub polar2cartesian(byref c as complex)
  ' ermittelt Real- und Imaginaerteil, wenn die Polarkoordinaten bekannt sind
  dim as complex ret
  c.real = c.abs * cos(c.arg)
  c.imag = c.abs * sin(c.arg)
end sub

sub cartesian2polar(byref c as complex)
  ' ermittelt die Polarkoordinaten, wenn Real- und Imaginaerteil bekannt sind
  dim as complex ret
  c.abs = sqr(c.real^2 + c.imag^2)
  if c.abs = 0 then
    c.arg = -1
  elseif c.imag >= 0 then
    c.arg = acos(c.real/c.abs)
  else
    c.arg = -acos(c.real/c.abs)
  end if
end sub


'#############################################
'#  Winkel zwischen zwei Strecken berechnen  #
'#############################################
const as double Rad2Deg = 45.0/atn(1)
'dim as short mittex = 100, mittey = 100
'dim as short startx = 40, starty = 50
'dim as short zielx = 180, ziely = 80
dim as short mittex = 100, mittey = 100
dim as short startx = 40, starty = 40
dim as short zielx = 150, ziely = 30
dim as double winkel
dim as complex a, b

a = newCartesian(mittex-startx, mittey-starty)
b = newCartesian(mittex-zielx, mittey-ziely)
winkel = (a.arg-b.arg)*Rad2Deg
if winkel < 0 then winkel += 360

screen 18
line (mittex, mittey)-(startx, starty)
line (mittex, mittey)-(zielx, ziely)
print winkel
sleep