Convert Rectangle to Triangle and display x,y coordinates
Mostrar comentarios más antiguos
Hi,
User will enter the length (L) and width (W) of a rectangle. User will be then asked to enter the number of rows (R) and columns (C).
Code should divide the rectangle as below:
- Length divided by number of columns.
- Width divided by number of rows.
- We get some number of small rectangles.
- Each small rectangle must be divided into half resulting in 2 triangles.
- Program must finally display the total number of triangles and it should also display the x and y coordinates (x,y) for each edges of triangle.
Please check the below picture for reference:

For e.g.: If user enters L=90 and W=50, R=2 and C=2.
Program should display the below:
- Total number of triangles = 8
- T1(a) = (0, 0)
- T1(b) = (45, 0)
- T1(c) = (0, 45)
- T2(a) = (45, 0)
- T2(b) = (45, 25)
- T2(c) = (0, 45)
- T3(a) = (45, 0)
- T3(b) = (90, 0)
- T3(c) = (45, 25)
- and so on…
Please guide me. I am sorry if the solution is very simple, as I am new to Matlab.
Thanks in advance.
Regards, Fahim
1 comentario
Guillaume
el 13 de Abr. de 2017
Note: edges don't have coordinates. vertices do.
Respuestas (2)
Image Analyst
el 13 de Abr. de 2017
Start with this snippet to ask a user for numbers:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
1 comentario
Image Analyst
el 16 de Abr. de 2017
Mohammed, in your latest comment to your "Answer" you didn't even try to use the code I gave you. Nowhere in that code do you have inputdlg(). So, here, I've added the third and fourth prompts and edit fields (rows and columns) to ask the user. Simply copy and paste this code and run it.
% Ask user for four numbers.
defaultValue = {'90', '50', '2', '2'};
titleBar = 'Enter a value';
userPrompt = {'Enter the Length (L): ', 'Enter the Width (W): ', 'Enter the number of rows (R): ', 'Enter the number of columns (C): '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
usersValue3 = str2double(caUserInput{3})
usersValue4 = str2double(caUserInput{4})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
% Do validation checks for 3 and 4, then if they pass, assign the variables:
% Don't use length and width as the variable namse because they are the names of built in functions.
triLength = usersValue1;
triWidth = usersValue2;
rows = usersValue3;
columns = usersValue4;
Mohammed Fahimuddin Mulla
el 14 de Abr. de 2017
0 votos
2 comentarios
Image Analyst
el 14 de Abr. de 2017
My code can be adapted to ask for 4 numbers instead of 2 - it's really rather straightforward. If you can't figure out how to ask for 4 numbers instead of 2, let me know.
Next, read the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F about creating variable numbers of variables - a very bad idea in general.
Mohammed Fahimuddin Mulla
el 16 de Abr. de 2017
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!