Das Programm berechnet die Fläche eines Dreiecks, wenn die Koordinaten seiner drei Eckpunkte bekannt sind. Das Dreieck kann sowohl im Raum als auch auf einer Ebene definiert sein. Das gegebene Dreieck wird durch Betätigen des Schalters „Fläche des Dreiecks“ grafisch im Fenster dargestellt und seine Fläche wird berechnet. Das entsprechende MATLAB-Skript wird nachstehend dargestellt.
function Dreieck
% Kreieren des Figure-Windows
fg = figure('visible', 'off', 'position', [400,200,750,450]);
% Anlegen der einzelnen Grafik-Komponenten
% Statischer Text
htext1 = uicontrol('style', 'text', 'string', 'Punkt A', ...
'position', [500, 400, 80, 25]);
htext2 = uicontrol('style', 'text', 'string', 'Punkt B', ...
'position', [500, 370, 80, 25]);
htext3 = uicontrol('style', 'text', 'string', 'Punkt C', ...
'position', [500, 340, 80, 25]);
% Punkt A
axh = uicontrol('style', 'edit', 'string', ...
'Ax', 'position', [610, 400, 40, 25]);
ayh = uicontrol('style', 'edit', 'string', ...
'Ay', 'position', [660, 400, 40, 25]);
azh = uicontrol('style', 'edit', 'string', ...
'Az', 'position', [710, 400, 40, 25]);
% Punkt B
bxh = uicontrol('style', 'edit', 'string', ...
'Bx', 'position', [610, 370, 40, 25]);
byh = uicontrol('style', 'edit', 'string', ...
'By', 'position', [660, 370, 40, 25]);
bzh = uicontrol('style', 'edit', 'string', ...
'Bz', 'position', [710, 370, 40, 25]);
% Punkt C
cxh = uicontrol('style', 'edit', 'string', ...
'Cx', 'position', [610, 340, 40, 25]);
cyh = uicontrol('style', 'edit', 'string', ...
'Cy', 'position', [660, 340, 40, 25]);
czh = uicontrol('style', 'edit', 'string', ...
'Cz', 'position', [710, 340, 40, 25]);
% Ein Bedienknopf fuer die Flaeche
flaeche = uicontrol('style', 'pushbutton', 'string', 'Flaeche des Dreiecks', ...
'position', [500, 300, 170, 25], ...
'callback', (@flaechebutton_callback));
% Statischer Text: Flaeche =
textbox1 = uicontrol('style', 'text', 'string', 'kein Ergebnis',...
'position', [500, 270, 170, 25]);
% Statischer Text
hname = uicontrol('style', 'text', ...
'string', 'Autor: Vadim Anishchenko, M.Eng.', ...
'position', [500, 40, 200, 25]);
% Die Achsen anpassen
achsen = axes('units', 'pixels', 'position', [90, 60, 350, 350]);
% Figure-Window einen Namen zuweisen
set(fg, 'name', 'Dreieck');
movegui(fg, 'center') % Bedienoberflaeche auf Bildschirm zentrieren
set(fg, 'visible', 'on'); % Bedienoberflaeche sichtbar machen
% 'callbacks' der Druckknoepfe
function flaechebutton_callback(source, eventdata)
% Berechnung der Flaeche
% Daten anlegen
ax = str2double(get(axh, 'string'));
ay = str2double(get(ayh, 'string'));
az = str2double(get(azh, 'string'));
bx = str2double(get(bxh, 'string'));
by = str2double(get(byh, 'string'));
bz = str2double(get(bzh, 'string'));
cx = str2double(get(cxh, 'string'));
cy = str2double(get(cyh, 'string'));
cz = str2double(get(czh, 'string'));
ab=[bx-ax, by-ay, bz-az];
ac=[cx-ax, cy-ay, cz-az];
xVar=[ax, bx, cx, ax];
yVar=[ay, by, cy, ay];
zVar=[az, bz, cz, az];
plot3(xVar, yVar, zVar, '.', 'MarkerSize', 10, 'LineStyle', '-');
grid on % Gitternetzlinien einschalten
title('Dreieck'); % Ueberschrift des Diagramms
xlabel('X'); % Beschriftung der X-Achse
ylabel('Y'); % Beschriftung der Y-Achse
zlabel('Z'); % Beschriftung der Z-Achse
p=cross(ab, ac);
S = norm(p)/2;
set(textbox1, 'string', S); %Flaeche im GUI ausgeben
end
end



Keine Kommentare:
Kommentar veröffentlichen