Clear Filters
Clear Filters

Write a function called draw_constellations that takes no input arguments and returns no output arguments. Instead, it obtains its input via the function ginput.

4 views (last 30 days)
Write a function called draw_constellations that takes no input arguments and returns no output arguments. Instead, it obtains its input via the function ginput. See the previous problem for an explanation of how ginput works. Like the function in the previous problem, this function allows the user to plot points by clicking in a figure whose horizontal and vertical ranges are set to be 0 to 100, but with this function the plot symbol '*' is used, and the points are joined by straight lines. Furthermore, the color of the plotted symbols and lines must be white, and before plotting begins, the function must use the following two commands to set the background to black: set(gcf,'color','k') and set(gca,'color','k'), and it must issue the command axis square. Then, the function must enter a while-loop to plot the first point and then plot subsequent points with lines joining each point to the previous point. These connected points represents a constellation (more precisely, a star pattern). If the userclicks the N-key, with or without the Shift key depressed, then a new set of connected points is begun—not connected to the previous connected pattern. Finally, when the user hits the Q-key (with or without Shift), the loop must end and the function must return. Figure 2.43 is an example of the result of two connected patterns showing the use of draw_constellations to draw the constellations Ursa Major and Ursa Minor.
The problem my code is giving that it is not printing line but only marker.
function constelation
figure
hold on
axis([0 100 0 100]);
set(gca,'color','k');
set(gcf,'color','k');
while 1
a = 1;
[x y button] = ginput(a);
switch button
case 1
plot(x,y,'r-');
case {'n','N'}
a = 2;
case { 'q','Q'}
break;
end
end

Accepted Answer

Adam Danz
Adam Danz on 13 Apr 2020
Create the line object prior to the while-loop using NaN values for the x and y data. Then update the x and y data within the loop.
h = plot(nan, nan, 'r-o'); % Set the line properties here, too
while 1
. . .
a = 1;
[x y button] = ginput(a);
set(h,'XData', [h.XData, x], 'YData', [h.YData, y]) % Update line coordinates
. . .
end
h.XData(1) = []; % remove initial NaN
h.YData(1) = []; % remove initial NaN
  2 Comments
Abhishek Tyagi
Abhishek Tyagi on 19 Jan 2021
function draw_constellations
axis([0 100 0 100]);
hold on;
set(gca,'color','k');
set(gcf,'color','k');
h = plot(nan, nan, 'w-*');
axis square;
while true
a=1;
[x, y, button]=ginput(a);
set(h,'XData', [h.XData, x], 'YData', [h.YData, y]);
switch button
case 1
plot(x,y,'w-');
case {'q','Q'}
return;
case {'n','N'}
a=2;
end
end
end
i am able to create only one pattern after pressing 'n' it continues in last pattern instead of starting another
Adam Danz
Adam Danz on 19 Jan 2021
What is this line for? It's redundant.
plot(x,y,'w-');
Look at the value of "button" when you press p, Q, n, N, mouse, or whatever. It doesn't return a letter. It returns a number.

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 13 Apr 2020
muhammad - every time that you call plot, you create a new graphics object and the previous one will be removed from the axes. You would need to use hold on to retain the current plot(s) when adding a new one. But since you allow multiple constellations to be drawn, I would consider using a single plot for each constellation. Everytime the user selects a new star for that constellation, you would just update it. For example,
hConstellation = plot(NaN,NaN, 'w*-');
while 1
a = 1;
[x y button] = ginput(a);
set(hConstellation, 'XData', [get(hConstellation,'XData') x], 'YData', [get(hConstellation, 'YData') y]);
end
The trick is then how do you reset hConstellation for a new constellation? From ginput, button
Mouse buttons are indicated by 1 for the left button, 2 for the middle, and 3 for the right. Keys on the keyboard are indicated by their corresponding ASCII numbers
So you will need to use the ASCII numbers (in your switch statement) to capture whether n or N (or q or Q) have been pressed....not 'n' or 'N' (etc.).

Tags

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!