In: Computer Science
Using matlab:
Build a graphical user interface (GUI) the will read the two corners of a rectangle (from the GUI interface) and show the following:
The interface should have at least the following:
function varargout = Rectanglex(varargin)
% RECTANGLEX MATLAB code for Rectanglex.fig
% RECTANGLEX, by itself, creates a new RECTANGLEX or raises the
existing
% singleton*.
%
% H = RECTANGLEX returns the handle to a new RECTANGLEX or the
handle to
% the existing singleton*.
%
% RECTANGLEX('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in RECTANGLEX.M with the given input
arguments.
%
% RECTANGLEX('Property','Value',...) creates a new RECTANGLEX or
raises the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before Rectanglex_OpeningFcn gets called.
An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to Rectanglex_OpeningFcn via
varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only
one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Rectanglex
% Last Modified by GUIDE v2.5 17-Feb-2019 10:07:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Rectanglex_OpeningFcn, ...
'gui_OutputFcn', @Rectanglex_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Rectanglex is made visible.
function Rectanglex_OpeningFcn(hObject, eventdata, handles,
varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Rectanglex (see VARARGIN)
% Choose default command line output for Rectanglex
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Rectanglex wait for user response (see
UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command
line.
function varargout = Rectanglex_OutputFcn(hObject, eventdata,
handles)
% varargout cell array for returning output args (see
VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
%inputs
P1x = str2double(get(handles.P1x,'String'));
P1y = str2double(get(handles.P1y,'String'));
P2x = str2double(get(handles.P2x,'String'));
P2y = str2double(get(handles.P2y ,'String'));
w = abs(P1x - P2x);
h = abs(P1y-P2y);
%diagonal
%plot([P1x P2x],[P1y P2y])
%hold on
%centroid
Cx = (P1x+P2x)/2;
Cy = (P1y+P2y)/2;
plot(Cx,Cy,'x r')
hold on
Xpos = P1x;
Ypos = P1y;
if P1x>P2x
Xpos = P2x;
Ypos = P2y
end
rectangle('Position',[Xpos Ypos w h])
%axis([(min(P1x,P2x)-2) (max(P1x,P2x)+2) (min(P1y,P2y)-2)
(max(P1x,P2x)+2)])
xlim([(min(P1x,P2x)-2) (max(P1x,P2x)+2)])
ylim([(min(P1y,P2y)-2) (max(P1y,P2y)+2)])
legend('centroid')
grid on
title('Rectangle')
%Area & circumference
%function handle
Area = @(x,y) x*y;
Circumference = @(x,y) 2*(x+y);
%outputs
set(handles.Area, 'String', Area(w,h));
set(handles.Circumference, 'String', Circumference(w,h));
function P1y_Callback(~, eventdata, handles)
% hObject handle to P1y (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of P1y as
text
% str2double(get(hObject,'String')) returns contents of P1y as a
double
% --- Executes during object creation, after setting all
properties.
function P1y_CreateFcn(hObject, eventdata, handles)
% hObject handle to P1y (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all CreateFcns
called
% Hint: edit controls usually have a white background on
Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function P2x_Callback(hObject, eventdata, handles)
% hObject handle to P2x (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of P2x as
text
% str2double(get(hObject,'String')) returns contents of P2x as a
double
% --- Executes during object creation, after setting all
properties.
function P2x_CreateFcn(hObject, eventdata, handles)
% hObject handle to P2x (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all CreateFcns
called
% Hint: edit controls usually have a white background on
Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function P1x_Callback(hObject, eventdata, handles)
% hObject handle to P1x (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of P1x as
text
% str2double(get(hObject,'String')) returns contents of P1x as a
double
% --- Executes during object creation, after setting all
properties.
function P1x_CreateFcn(hObject, eventdata, handles)
% hObject handle to P1x (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all CreateFcns
called
% Hint: edit controls usually have a white background on
Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function P2y_Callback(hObject, eventdata, handles)
% hObject handle to P2y (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of P2y as
text
% str2double(get(hObject,'String')) returns contents of P2y as a
double
% --- Executes during object creation, after setting all
properties.
function P2y_CreateFcn(hObject, eventdata, handles)
% hObject handle to P2y (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all CreateFcns
called
% Hint: edit controls usually have a white background on
Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER
PLEASE PLEASE GIVE A THUMBS UP