function ret=checkob(blx, bly, trx, trty) global robot hit=0 if(robot.blx >=blx & robot.bly >= bly & robot.blx <= trx & robot.bly <= trty) hit=1 end if(robot.trx >=blx & robot.trty >= bly & robot.trx <= trx & robot.trty <= trty) hit=1 end if(robot.blx >=blx & robot.trty >= bly & robot.blx <= trx & robot.trty <= trty) hit=1 end if(robot.trx >=blx & robot.bly >= bly & robot.trx <= trx & robot.bly <= trty) hit=1 end ret=hit global robot axis([0,25,0,25]) hold ob(1)=struct('blx',5, 'bly',5, 'trx',15, 'trty',7, 'width',10, 'height',2) ob(2)=struct('blx',5, 'bly',5, 'trx',7, 'trty',15, 'width',2, 'height',10) ob(3)=struct('blx',5, 'bly',13, 'trx',15, 'trty',15, 'width',10, 'height',2) ob1=rectangle('Position', [ob(1).blx, ob(1).bly, ob(1).width, ob(1).height]) ob2=rectangle('Position', [ob(2).blx, ob(2).bly, ob(2).width, ob(2).height]) ob3=rectangle('Position', [ob(3).blx, ob(3).bly, ob(3).width, ob(3).height]) robot=struct('xpos', 14, 'ypos', 3, 'blx', 13.5, 'bly', 2.5, 'trx', 14.5, 'trty', 3.5) target=struct('xpos', 9, 'ypos', 11) r = plot(robot.xpos,robot.ypos,'o') t = plot(target.xpos,target.ypos,'x') set (r,'EraseMode', 'xor') velocity = 0.1 hittarget = 0 while (hittarget == 0) drawnow vx = target.xpos - robot.xpos vy = target.ypos - robot.ypos if ((vx >= -0.1) & (vx <= 0.1) & (vy>=-0.1) & (vy <= 0.1)) hittarget = 1 else unitconst = sqrt(vx^2 + vy^2) uvx = vx / unitconst uvy = vy / unitconst robot.xpos = robot.xpos + (uvx * velocity) robot.ypos = robot.ypos + (uvy * velocity) robot.blx = robot.xpos - .5 robot.bly = robot.ypos - .5 robot.trx = robot.xpos + .5 robot.trty = robot.ypos + .5 hit1=checkob(ob(1).blx, ob(1).bly, ob(1).trx, ob(1).trty) hit2=checkob(ob(2).blx, ob(2).bly, ob(2).trx, ob(2).trty) hit3=checkob(ob(3).blx, ob(3).bly, ob(3).trx, ob(3).trty) if(hit1 == 1) uvx = 2 uvy = -1 end if(hit2 == 1) uvx = 2 uvy = 1 end if(hit3 == 1) uvx = 2 uvy = -1 end robot.xpos = robot.xpos + (uvx * velocity) robot.ypos = robot.ypos + (uvy * velocity) robot.blx = robot.xpos - .5 robot.bly = robot.ypos - .5 robot.trx = robot.xpos + .5 robot.trty = robot.ypos + .5 set(r,'XData',robot.xpos,'YData',robot.ypos) end end