Convert Rectangle to Triangle and display x,y coordinates

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:
  1. Length divided by number of columns.
  2. Width divided by number of rows.
  3. We get some number of small rectangles.
  4. Each small rectangle must be divided into half resulting in 2 triangles.
  5. 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

Answers (2)

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 Comment

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;

Sign in to comment.

Thanks Guillaume and Image Analyst.
@ Guillaume, yes they are vertices.
@ Image Analyst, I am sorry if I have put my question in a wrong way. My requirement is when the program is run, following should be the ouput:
  • Enter the Length (L):
  • Enter the Width (W):
  • Enter the number of rows (R):
  • Enter the number of columns (C):
% If the user enters: L=90, W=50, R=2, C=2, then the output will be as 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)
  • % so on...until T8 vertices are displayed
Please refer the attached picture in my first post for reference. I hope I have made my query clear.
Thanks in advance.
Regards, Fahim

2 Comments

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.
Hi Image Analyst,
Thanks again for your kind support.
Below is the initial code I am using: Len=input('Enter the Length: ') % Length of the rectangle Wid=input('Enter the Width: ') % Width of the rectangle
row=input('Enter the number of rows: ') % No of rows col=input('Enter the number of columns: ') % No of columns
e=row*col*2; % No of elements nn=(row+1)*(col+1); % No of nodes
Now the output must be as 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)
  • % so on...until T8 vertices are displayed
I am still confused using your code.
Thanks in advance.
Regards, Fahim

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!