Related
I'm trying to draw a hexagonal grid with love2d using love.graphics.line but after drawing around 15000 lines the program crashes with only one message abort (core dumped).
function love.load()
ww, wh = love.graphics.getDimensions()
hexradius = 10
size = 50
hexgrid = newGrid(hexradius, size, size)
end
function love.draw(dt)
drawgrid()
end
function drawgrid()
local jxOffset = hexgrid.rad * -math.tan(math.pi/1.5)
local ixOffset = jxOffset/4
local iyOffet = jxOffset * math.sin(math.pi/3)
for i=1,hexgrid.size.x do
for j=1,hexgrid.size.y do
love.graphics.push()
love.graphics.translate(ixOffset + j * jxOffset, i * iyOffet)
love.graphics.line(
hexgrid.hex[1].x, hexgrid.hex[1].y,
hexgrid.hex[2].x, hexgrid.hex[2].y,
hexgrid.hex[3].x, hexgrid.hex[3].y,
hexgrid.hex[4].x, hexgrid.hex[4].y,
hexgrid.hex[5].x, hexgrid.hex[5].y,
hexgrid.hex[6].x, hexgrid.hex[6].y,
hexgrid.hex[1].x, hexgrid.hex[1].y)
love.graphics.pop()
end
ixOffset = -ixOffset
end
end
function newGrid(rad, xsize, ysize)
local g = {
rad = rad,
hex = {},
size = {
x = xsize,
y = ysize,
},
}
for i=1,6 do
local dir = math.pi/3 * (i+0.5)
g.hex[i] = {}
g.hex[i].x = g.rad * math.cos(dir)
g.hex[i].y = g.rad * math.sin(dir)
end
return g
end
I'm quite new to love2d and graphical stuff in general so maybe I'm trying to draw a great amount of lines and it's just not possible, but the grid I'm generating does not appear to be that big. I'm using LOVE 11.3.
Thanks in advance!
I'd try drawing with a polygon instead, see if you have better results.
love.graphics.polygon( mode, vertices )
https://love2d.org/wiki/love.graphics.polygon
for i = 1, hexgrid.size.x do
for j = 1, hexgrid.size.y do
love.graphics.push()
love.graphics.translate( ixOffset +j *jxOffset, i *iyOffet )
love.graphics.polygon( 'line',
hexgrid.hex[1].x, hexgrid.hex[1].y,
hexgrid.hex[2].x, hexgrid.hex[2].y,
hexgrid.hex[3].x, hexgrid.hex[3].y,
hexgrid.hex[4].x, hexgrid.hex[4].y,
hexgrid.hex[5].x, hexgrid.hex[5].y,
hexgrid.hex[6].x, hexgrid.hex[6].y )
love.graphics.pop()
end
...
Alternatively, circles drawn with optional segment parameter set # 6, draws hexagons.
love.graphics.circle( mode, x, y, radius, segments )
https://love2d.org/wiki/love.graphics.circle
for i = 1, hexgrid.size.x do
for j = 1, hexgrid.size.y do
love.graphics.circle( 'line', ixOffset +j *jxOffset, i *iyOffet, hexradius, 6 )
end
...
As Luther suggested in a comment it's a bug in Löve. It's already fixed and will be available in the next release Löve 11.4.
Thanks!
I have function called Assignment in Matlab with PsychToolBox. This function shows a random color to the paritcipant and require participant to name the color and record this data.
function should return me 2 output as a string
rgb code of the random color like: trial(1).color = [5 5 5]
a matrix which correspond to the sound record.
I write the main functions and color part is okay, but I cannot integrate the recording function into the main function.
in main function I use this string trial.data = recording(1,0,5)
and then I wrote a subfunction named "recording"
function recording (wavfilename, voicetrigger, maxsecs)
bla, bla
end
However, the main function does not recognize the subfunction. Am I doing an logical error? the error message is below
Error: File: assignment.m Line: 40 Column: 27
Unexpected MATLAB expression.
line 40 = trial.data = recording(1,0,5)
function ass8(trial)
Screen('Preference', 'SkipSyncTests', 1)
ListenChar(2);
Screen('HideCursorHelper', 0, 0)
[myWin, rect]=Screen('OpenWindow',0,[128,128,128]);
centerX=rect(3)/2;
centerY=rect(4)/2;
for trial = 1:100
Screen('TextSize', myWin, 30);
Screen('TextFont', myWin, 'Times');
[normBoundsRect, offsetBoundsRect] = Screen('TextBounds',myWin, 'What is the color of the rectangle?');
Screen('DrawText', myWin, 'What is the color of the rectangle?', (centerX-(normBoundsRect(3)/2)),(centerY-(normBoundsRect(4)/2+150)), [0,0,0]);
Screen('Flip', myWin)
WaitSecs(1)% inter stimulus interval
color = randi(255,1,3)
while 1
Screen('FillRect', myWin, color ,[583, 284, 783, 484])
% [ (centerX-100), (centerY-100), (centerX+100),(centerY+100)]);
Screen('Flip', myWin)
WaitSecs(3)
trial.color = color % trial 'ın rengini belirtmesini söyledim
trial.data = reco(1,0 5)% trial'ın ismi 1, kayıt yapacağı süre ise 3 sn
if Waitsecs(3)==1
break; % Terminates the loop if the condition is % satisfied
end
end
pause(.05);
% [clicks, x, y, buttons] = GetClicks(myWin);
%
% buttons=0;
% while ~buttons
% [x, y, buttons] = GetMouse(myWin);
% end
% while 1
% [x,y,buttons] = GetMouse(myWin);
% if ~buttons(1)
% break;
% end
% end
Screen('CloseAll')
end
end
function reco(wavfilename, voicetrigger, maxsecs)
%
% AssertOpenGL;
if nargin < 1
wavfilename = [];
end
if nargin < 2
voicetrigger = [];
end
if isempty(voicetrigger)
voicetrigger = 0;
end
if nargin < 3
maxsecs = [];
end
if isempty(maxsecs)
maxsecs = inf;
end
InitializePsychSound;
freq = 44100;
pahandle = PsychPortAudio('Open', [], 2, 0, freq, 2);
PsychPortAudio('GetAudioData', pahandle, 10);
PsychPortAudio('Start', pahandle, 0, 0, 1);
if voicetrigger > 0
% Yes. Fetch audio data and check against threshold:
level = 0;
% Repeat as long as below trigger-threshold:
while level < voicetrigger
% Fetch current audiodata:
[audiodata offset overflow tCaptureStart] = PsychPortAudio('GetAudioData', pahandle);
% Compute maximum signal amplitude in this chunk of data:
if ~isempty(audiodata)
level = max(abs(audiodata(1,:)));
else
level = 0;
end
% Below trigger-threshold?
if level < voicetrigger
% Wait for a millisecond before next scan:
WaitSecs(0.0001);
end
end
else
% Start with empty sound vector:
recordedaudio = [];
end
s = PsychPortAudio('GetStatus', pahandle)
while ~KbCheck && ((length(recordedaudio) / s.SampleRate) < maxsecs)
% Wait a second...
WaitSecs(1);
% Query current capture status and print it to the Matlab window:
s = PsychPortAudio('GetStatus', pahandle);
% Print it:
fprintf('\n\nAudio capture started, press any key for about 1 second to quit.\n');
fprintf('This is some status output of PsychPortAudio:\n');
disp(s);
% Retrieve pending audio data from the drivers internal ringbuffer:
audiodata = PsychPortAudio('GetAudioData', pahandle);
nrsamples = size(audiodata, 2);
% Plot it, just for the fun of it:
plot(1:nrsamples, audiodata(1,:), 'r', 1:nrsamples, audiodata(2,:), 'b');
drawnow;
% And attach it to our full sound vector:
recordedaudio = [recordedaudio audiodata]; %#ok<AGROW>
end
PsychPortAudio('Stop', pahandle);
audiodata = PsychPortAudio('GetAudioData', pahandle);
recordedaudio = [recordedaudio audiodata];
PsychPortAudio('Close', pahandle);
if ~isempty(wavfilename)
psychwavwrite(transpose(recordedaudio), 44100, 16, wavfilename)
end
fprintf('helal lan!\n');
ListenChar(2);
end
I have my own macro defined in the Sheet1, for example:
Function MyPower(rad As Double) As Double
Dim res As Double
If rad = 0 Then
res = 0
Else
res = 0.01 * Exp(1.7 * Log(rad))
Power = res
End If
End Function
I want to be able to invoke it via a formula bar like one of the standard functions, i.e. fx = MyPower("A1"), the same way as fx = Cos("A1"), for example.
What do I need to do to add this macro to a formula bar? I am sure it's very simple, but it does not work by default.
Simple! Create your function in a Module instead of a Sheet macro.
You also mis-placed and mis-typed the return value...
Function MyPower(rad As Double) As Double
Dim res As Double
If rad = 0 Then
res = 0
Else
res = 0.01 * Exp(1.7 * Log(rad))
End If
MyPower = res ' <-- "MyPower": Move it here
End Function
What I am trying to do is create a handwriting application which allows a person to press on the object (circle) and move it up/down on a set path. If the user reaches the lowest point another circle object is created and another set of points is created to follow etc.
So far I have a onTouch event which moves my ImageView object (circle) to where ever the finger is on the touch screen.
https://gist.github.com/Temptex/9796403
How can i get my ImageView (Circle) object to go from Point A -> B smoothly using onTouch events?
Edit: A picture example of what I am asking: http://imgur.com/kjgGcba
created an algorithm for it.
Created an array of x integers and y integers and used a for loop to check if the object was to high (out of bounds in the y coordinates) and check if y and x were out of bounds in the arrays.
If it was out of bounds from the array of integers, set it to the current x/y array values. in the for loop.
If this helps anyone here is the code:
private final int yCoOrdinate[] = {310, 360, 410};
private final int xCoOrdinate[] = {905, 890, 875};
// Get finger position
int x = (int)event.getRawX();
int y = (int)event.getRawY();
// View x/y co-ordinate on TextView
coordinates.setText("X = " + x + " - Y = " + y);
for(int i = 0; i < yCoOrdinate.length; i++) {
// If object is to high set it to yCoOrdinate[0]
if(y <= yCoOrdinate[0]) {
y = yCoOrdinate[0];
}
// Checks top left corner of A
if(y == yCoOrdinate[0] && x <= xCoOrdinate[0]) {
x = xCoOrdinate[0];
Log.d("Coordinate check", "X = " + x + "Y = " + y);
// Checks if current x/y position if out of bounds and sets them to i
} else if (y <= yCoOrdinate[i] && x <= xCoOrdinate[i]) {
y = yCoOrdinate[i];
x = xCoOrdinate[i];
Log.d("Coordinate check", "X = " + x + "Y = " + y);
}
}
I can now control my ImageView with in bounds.
The following code is a graphic rendering of the Earth globe spinning eastward. I have two push buttons, Spin and Stop. Both share the same callback function, hAnimaCallback. In particular, this latter does not work. I pretend to use the strings (names) of the push buttons to create a switch that stops the movement. However, I can not get to change the string name inside the while loop and I do not understand why my method is wrong.
function example
fh = figure('Menu','none','Toolbar','none','Units','characters');
T = 0:pi/100:2*pi;
Y = zeros(numel(T),3);
Y(:,1) = 7000*cos(T);
Y(:,2) = 7000*sin(T);
hPanAni = uipanel('parent',fh,'Units','characters','Position',...
[22.6 10.4 53 23],'title','Controls','FontSize',11,...
'FontAngle','italic','FontWeight','bold');
hIniAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
'Position',[0.14 0.64 0.5 0.12],'String','Spin',...
'FontSize',10,'Callback',#hAnimaCallback);
hFinAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
'Position',[0.14 0.26 0.5 0.12],'String','Stop',...
'FontSize',10,'Callback',#hAnimaCallback);
hPantSim = uipanel('Parent',fh,'Units','characters',...
'Position',[107.87 8 157.447 42],'BorderType','none','title',...
'Screen','FontSize',11,'FontAngle','italic',...
'FontWeight','bold');
hPantSimInt = uipanel('Parent',hPantSim,'Units','normalized','Position',...
[0 0 1 1],'BorderType','line','BackgroundColor','black');
ah4 = axes('Parent',hPantSimInt,'Units','normalized','Position',...
[0 0 1 1],'Color','none','Visible','off','DataAspectRatio',...
[1 1 1],'NextPlot','add');
rotate3d(ah4);
hgrot = hgtransform('Parent',ah4);
Resf = 6378;
maptext = imread('tierra.jpg');
[X, map] = rgb2ind(maptext,128);
[x,y,z] = sphere(50);
x = Resf*x;
y = Resf*y;
z = Resf*z;
props.FaceColor= 'texture';
props.EdgeColor = 'none';
props.Parent = hgrot;
props.Cdata = flipud(X); % it is necesary to do this for getting the
% appropiate image on the sphere
hsurf = surface(x,y,z,props);
colormap(map);
axis equal;
view([71 14]);
set(gcf,'Renderer','opengl')
drawnow;
line('parent',ah4,'XData',Y(:,1),'YData',Y(:,2),'ZData',...
Y(:,3),'Color','red','LineWidth',2);
line('parent',ah4,'XData',Y(end,1),'YData',Y(end,2),...
'ZData',Y(end,3),'Marker','o','MarkerSize',6,'MarkerFaceColor','b');
axis square equal vis3d;
view(3);
handles.XLim = get(ah4,'XLim');
handles.YLim = get(ah4,'YLim');
handles.ZLim = get(ah4,'ZLim');
xmin = handles.XLim(1);
ymin = handles.YLim(1);
zmin = handles.ZLim(1);
xmax = handles.XLim(2);
ymax = handles.YLim(2);
zmax = handles.ZLim(2);
set(ah4, 'XLim', [xmin xmax],'YLim', [ymin ymax],'Zlim',[zmin zmax]);
az = 0;
function hAnimaCallback(hObject,evt)
while (ishandle(fh))
state = get(hObject,'String'), % state should change between the states of
% Spin and Stop but this does not occur
if (strcmp(state,'Stop'))
break;
else
az = az + 0.01745329252;
set(hgrot,'Matrix',makehgtform('zrotate',az));
drawnow;
end
end
end
end
It seems like you are running into some kind of race condition because of the repeated drawnow calls in the while loop. The state changes to Stop, when you press the Stop button, but it is to quick to notice. Basically, the while loop is running as fast as MATLAB can run. Instead of drawnow using a pause(0.1) command seems to work (with a little bit code logic changes):
az = 0;
spin = false;
function hAnimaCallback(hObject,~)
state = get(hObject,'String')
if strcmp(state, 'Spin')
spin = true;
else
spin = false;
end
while (spin)
az = az + 0.01745329252;
set(hgrot,'Matrix',makehgtform('zrotate',az));
pause(0.1);
end
end