Hints:
Avoid repeated code. Prefer:
brtn = 255 * get(handles.slider1, 'Value');
instead of writing 255*brtn whenever brtn is used. Typing "MousePos(1)" repeatedly is a source of typos also. What about:
x = MousePos(1);
y = MousePos(2);
The standard method for comments is to start with a % and a space. "%%Kreuz%%" looks fancy, but not Matlab'ish.
X(1:32) is faster than X([1:32]). The reason is not documented, but it seems like Matlab checks the limits for 1 and 32 only, while in the latter case the vector 1:32 is created explicitly and the limits are checked for each element. Remember that [] is the Matlab operator for concatenation, so there is no need to concat teh vector 1:32 with nothing else.
The actual question: Replace e.g.:
Transmit(MousePos(1),MousePos(2)) = 255*brtn;
Transmit(MousePos(1)+1,MousePos(2)) = 255*brtn;
if MousePos(1) > 1
Transmit(MousePos(1)-1,MousePos(2)) = 255*brtn;
end
if MousePos(2) > 1
Transmit(MousePos(1),MousePos(2)-1) = 255*brtn;
end
Transmit(MousePos(1),MousePos(2)+1) = 255*brtn;
by
xi = max( 1, x - 1);
xf = min(32, x + 1);
yi = max( 1, y - 1);
yf = min(32, y + 1);
Transmit(xi:xf, y) = brtn;
Transmit(x, yi:yf) = brtn;
An alternative approach:
T = zeros(40, 40);
x = MousePos(1) + 4;
y = MousePos(2) + 4;
...
Transmit = T(5:36, 5:36);
...
So you draw without considering the limits and crop out the visible area afterwards. I'd prefer this, because it is much easier to expand for other objects.
Best Answer