IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaces Graphiques Discussion :

Tracer un segment & déplacer les extrémités


Sujet :

Interfaces Graphiques

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2007
    Messages
    979
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2007
    Messages : 979
    Par défaut Tracer un segment & déplacer les extrémités
    Bonjour,

    voila je travail depuis ce matin sur une petit interface graphique pour pouvoir créer des scènes (images 2D qui représentent le plan d'un étage d'unbâtiment ). En gros ceci reviens à tracer des segments sur l'image (les murs) .

    Pour le moment, mon programme demande à l'utilisateur de faire deux clics sur l'image et il crée le segment correspondant ....

    Mon problème c'est que les deux extrémités ne sont pas déplaçable sur l'image . j'aimerai bien pouvoir déplacer une extrémité du segment par la souris, comment je peux faire cela avec matlab ?

    Merciii


    Ci-joint , le code de l'interface ( la première moitié est l'interface , la 2eme c'est une fonction qui crée un segment à partir de deux points)


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
     
    function scene_gui
    % function scene_gui
    % Scene Generation GUI
    % 
    %
    %
     
     
    % @ Init program 
    disp('init environement ...');
    clear all
    close all
     
    global GUI_DEBUG
    GUI_DEBUG = 1;
     
    %------------------------------------------------------------
    % @ Data Structure
     
    global scene_data;
     
    object = struct (...
        'points',[],...
        'params',[]);
    handle = struct (...
        'main',[],...
        'child',[]);
     
    scene_data = struct (...
        'matrix', [],...
        'currentObj',object,...
        'objects',[],...
        'handle',handle); % \TODO : complete this !! 
     
    %------------------------------------------------------------
    % @ Intialisation Figure
     
    init_handle = figure('units','pixels',...
        'position',[400 850 400 120],...
        'color',[1.0 1.0 1.0],...
        'numbertitle','off',...
        'MenuBar','none',...
        'name','Scene Generation Program : Intialisation',...
        'tag','init');
     
    % Size Manager Panel
    init_panel = uipanel('Title','Init Scene :',...
        'FontSize',8,...
        'BackgroundColor','white',...
        'Position',[0.05 0.05 0.9 0.9]);
     
    % Choose Height and Width 
    init_hedit = uicontrol('style','edit',...
        'position',[100 50 90 25],...
        'string','height',...
        'tag','mat_hsize');
    init_wedit = uicontrol('style','edit',...
        'position',[200 50 90 25],...
        'string','width',...
        'tag','mat_wsize');
     
    % Validate initialisation pushbutton
    init_boutton = uicontrol('style','pushbutton',...
        'units','normalized',...
        'position',[0.3 0.1 0.4 0.2],...
        'string','Ok',...
        'callback',@init_scene0,...
        'tag','init_run');
     
    %------------------------------------------------------------
    % GUI Data Struct 
    disp('init gui parameters ...');
    gui_data=guihandles(gcf);
    guidata(gcf,gui_data);
     
     
    %------------------------------------------------------------
    % @ Main Figure
     
     
     
    %============================================================
    % Functions : 
    %============================================================
     
    %------------------------------------------------------------
    % Scene Parameters Function
    function init_scene0(obj,event)
    global scene_data;
    gui_data = guidata(gcbf);
    h = str2num(get(gui_data.mat_hsize,'string'));
    w = str2num(get(gui_data.mat_wsize,'string'));
    if isempty(h)
       h = 50;
    end
    if isempty(w)
       w = 100;
    end  
    scene_data.matrix = zeros(h,w); 
     
    % Init Program Main Figure
    close gcbf
    %imagesc(scene_data.matrix);
    init_scene1();
     
     
    %------------------------------------------------------------
    % Scene Initialisation Function
    function init_scene1
    global scene_data;
     
    disp('init main window ...');
    % Main Figure
    main_handle = figure('units','pixels',...
        'position',[200 350 800 600],...
        'color',[1.0 1.0 1.0],...
        'numbertitle','off',...
        'name','Scene Generation Program : Main',...
        'tag','interface');
    scene_data.handle.main = main_handle;
    % Scene image
    subplot('Position',[0.02 0.2 0.96 0.8])
    image_1_handle = image(scene_data.matrix,...
        'tag','scene');
    axis image off
     
    % Create line
    annotation1 = annotation(main_handle,'line',[0.0 1.0],[0.19 0.19]);
     
    % Add Object pushbutton
    add_line_boutton = uicontrol('style','pushbutton',...
        'units','normalized',...
        'position',[0.3 0.02 0.3 0.05],...
        'string','Add object',...
        'callback',@add_object,...
        'tag','add_line');
     
    %
    % \TODO add settings Objects
    %
    gui_data=guihandles(gcf);
    guidata(gcf,gui_data);
     
     
     
    %------------------------------------------------------------
    % Add Object Function 
    function add_object(obj,event)
    global scene_data;
    scene_data.currentObj.points = ginput(2);
       %----------------------------------
       % Object Prameters Figure
       figure('units','pixels',...
            'position',[250 360 300 100],...
            'color',[1.0 1.0 1.0],...
            'numbertitle','off',...
            'MenuBar','none',...
            'name','Set Object Prameters',...
            'tag','init');
       uicontrol('style','edit',...
            'position',[50 20 90 25],...
            'string','parameter',...
            'tag','param_obj');
       init_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.5 0.1 0.4 0.3],...
            'string','Ok',...
            'callback',@set_param_obj,...
            'tag','set_param_obj');
       init_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.5 0.5 0.4 0.3],...
            'string','Cancel',...
            'callback',@cancel_param_obj,...
            'tag','set_param_obj');
       gui_data=guihandles(gcf);
       guidata(gcf,gui_data);
     
    %------------------------------------------------------------
    % Set Object Parameter Function 
    function set_param_obj(obj,event)
    global scene_data;
    gui_data = guidata(gcf);
    h = str2num(get(gui_data.param_obj,'string'));
    if isempty(h)
       h = 10.0;
    end
    scene_data.currentObj.params = h;
    close gcf
    store_object();
     
    % Cancel Object Parameter Function 
    function cancel_param_obj(obj,event)
    global scene_data;
    gui_data = guidata(gcf);
    close gcf
     
    % Add Object to Scene Function 
    function store_object()
    global scene_data;
    disp('storing object ...');
    gui_data = guidata(gcf);
    Sx = round(scene_data.currentObj.points(1,2));
    Sy = round(scene_data.currentObj.points(1,1));
    Ex = round(scene_data.currentObj.points(2,2));
    Ey = round(scene_data.currentObj.points(2,1));
    Coords = bresenham(Sx,Sy,Ex,Ey);
    scene_data.matrix(sub2ind(size(scene_data.matrix),Coords(:,1),Coords(:,2))) ...
        = scene_data.currentObj.params;
    scene_data.objects = [scene_data.objects scene_data.currentObj];
    set(gui_data.scene,'CDATA',scene_data.matrix);
    guidata(gcf,gui_data)
     
     
    function [Coords] = bresenham(Sx,Sy,Ex,Ey)
    % function [Coords]=brlinexya(Sx,Sy,Ex,Ey)
    % Bresenham line algorithm.
    % Sx, Sy : starts point
    % Ex, Ey : end points
    % Coords : ordered list of x,y coords.
    % 
    DEBUG = false;
     
    if nargin < 4,
        % Demo Mode
        DEBUG = true;
        pxl = 50;
        mymat = zeros(pxl,pxl);
        if DEBUG 
            disp('This is a demo.')
        end
     
     
        imagesc(mymat); axis image;
        if DEBUG
            disp('Click two points on the image.')
        end
        coordinates = ginput(2);
        coordinates
        Sx = round(coordinates(1,2));
        Sy = round(coordinates(1,1));
        Ex = round(coordinates(2,2));
        Ey = round(coordinates(2,1));
    end
     
    	Dx = Ex - Sx;
    	Dy = Ey - Sy;
    	CoordsX = zeros(2 .* ceil(abs(Dx)+abs(Dy)),1);
    	CoordsY = zeros(2 .* ceil(abs(Dx)+abs(Dy)),1);
        iCoords=0;
    	if(abs(Dy) <= abs(Dx))
    		if(Ey >= Sy)
    			if(Ex >= Sx)
    				D = 2*Dy - Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X < Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			else % Ex < Sx
    				D = -2*Dy - Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X > Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			end
    		else % Ey < Sy
    			if(Ex >= Sx)
    				D = 2*Dy + Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X < Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			else % Ex < Sx
    				D = -2*Dy + Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X > Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			end
    		end
    	else % abs(Dy) > abs(Dx) 
    		Tmp = Ex;
    		Ex = Ey;
    		Ey = Tmp;
    		Tmp = Sx;
    		Sx = Sy;
    		Sy = Tmp;
    		Dx = Ex - Sx;
    		Dy = Ey - Sy;
    		if(Ey >= Sy)
    			if(Ex >= Sx)
    				D = 2*Dy - Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X < Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			else % Ex < Sx
    				D = -2*Dy - Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X > Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			end
    		else % Ey < Sy
    			if(Ex >= Sx)
    				D = 2*Dy + Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X < Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			else % Ex < Sx
    				D = -2*Dy + Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X > Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			end
    		end
        end
     
    Coords = [CoordsX(1:iCoords),CoordsY(1:iCoords)];
     
    if DEBUG
       mymat(sub2ind(size(mymat),Coords(:,1),Coords(:,2))) = 1;
       imagesc(mymat);
    end

  2. #2
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    function test
     
    figure('WindowButtonMotionFcn',[],...
        'WindowButtonupfcn',@wbufcn);
     
    axes('units','normalized','position',[0 0 1 1],...
        'xlim',[0 1],'ylim',[0 1]);
     
    p=plot(rand(1,2),rand(1,2),'r-o','buttondownfcn',@bdfcn);
    xlim([0 1])
    ylim([0 1])
     
        function bdfcn(obj,event)
     
            set(gcbf,'WindowButtonMotionFcn',@wbmfcn);
     
        end
     
        function wbmfcn(obj,event)
     
            x=get(p,'xdata');
            y=get(p,'ydata');
     
            cp=get(gca,'currentpoint');
            cp=cp(1,1:2);
     
            d=sqrt((x-cp(1)).^2+(y-cp(2)).^2);
            [pasbesoin,idx]=min(d);
     
            x(idx)=cp(1);
            y(idx)=cp(2);
     
            set(p,'xdata',x,'ydata',y)
            xlim([0 1])
            ylim([0 1])
     
        end
     
        function wbufcn(obj,event)
     
            set(gcbf,'WindowButtonMotionFcn',[]);
     
        end
     
    end

  3. #3
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2007
    Messages
    979
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2007
    Messages : 979
    Par défaut
    Exactactement , c'est super merci .

    Je dois pouvoir bien avancer avec ton code Dut,

    encore merci .

  4. #4
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2007
    Messages
    979
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2007
    Messages : 979
    Par défaut
    Salut,

    J'ai une petite version qui tiens la route .

    Fonctionnalités :
    - Choix de la taille de l'image.
    - Ajout de segments dans l'image, avec un paramètre.
    - fonctionnalité 'Undo' pour éliminer le dernier segment.
    - Sauvegarde du résultats dans un .mat

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    function scene_gui_3
    % function scene_gui
    % Scene Generation GUI
    % 
    %
    % last change : 14/02/2008
     
     
     
    clear all
    close all
     
    % Verbose Mode
    global GUI_DEBUG
    GUI_DEBUG = 1;
     
    % Init program 
    sprint('init environement ...');
     
    % Data Structure
    global scene_data;
     
    object = struct (...
        'points',[],...
        'params',[],...
        'p',[]);
     
    handle = struct (...
        'main',[],...
        'child',[]);
     
    scene_data = struct (...
        'matrix', [],...
        'currentObj',object,...
        'objects',[],...
        'handle',handle);
     
    %------------------------------------------------------------
    % @ Intialisation Figure
     
    init_handle = figure('units','pixels',...
        'position',[400 850 400 120],...
        'color',[1.0 1.0 1.0],...
        'numbertitle','off',...
        'MenuBar','none',...
        'name','Scene Generation Program : Intialisation',...
        'tag','init');
     
    % Size Manager Panel
    init_panel = uipanel('Title','Scene Size Manage :',...
        'FontSize',8,...
        'BackgroundColor','white',...
        'Position',[0.05 0.05 0.9 0.9]);
     
    % Choose Height and Width 
    init_hedit = uicontrol('style','edit',...
        'position',[100 55 80 20],...
        'string','height',...
        'tag','mat_hsize');
    init_wedit = uicontrol('style','edit',...
        'position',[200 55 80 20],...
        'string','width',...
        'tag','mat_wsize');
     
    % Validate initialisation pushbutton
    init_boutton = uicontrol('style','pushbutton',...
        'units','normalized',...
        'position',[0.25 0.2 0.45 0.18],...
        'string','Ok',...
        'callback',@init_scene0,...
        'tag','init_run');
     
    % GUI Data Struct 
    sprint('init gui parameters');
    gui_data=guihandles(gcf);
    guidata(gcf,gui_data);
     
     
    %============================================================
    % Functions : 
    %============================================================
     
      % Scene Parameters Function
      function init_scene0(obj,event)
        global scene_data;
        gui_data = guidata(gcbf);
        sprint('set scene size');
        h = str2num(get(gui_data.mat_hsize,'string'));
        w = str2num(get(gui_data.mat_wsize,'string'));
        if isempty(h)
           h = 50;
        end
        if isempty(w)
           w = 100; %50; %100;
        end  
        scene_data.matrix = zeros(h,w); 
        scene_data.currentObj.point = round([0.4*w 0.4*h ;0.6*w 0.6*h]);
        scene_data.currentObj.params = 20; % \TODO review this
        close gcbf
        %imagesc(scene_data.matrix);
        init_scene1();
      end
     
     
      %------------------------------------------------------------
      % Scene Initialisation Function
      function init_scene1
        global scene_data;
     
        sprint('init main window');
        % Main Figure
        main_handle = figure('units','pixels',...
            'position',[200 350 800 600],...
            'WindowButtonMotionFcn',[],...
            'WindowButtonupfcn',@wbufcn,...
            'numbertitle','off',...
            'name','Scene Generation Program : Main',...
            'tag','interface');
        scene_data.handle.main = main_handle;
        % Scene image
        subplot('Position',[0.02 0.2 0.96 0.8])
        hold on
          image_1_handle = image(scene_data.matrix,...
              'tag','scene');
          axis image
          p_handle = plot(scene_data.currentObj.point(:,1),...
          scene_data.currentObj.point(:,2),...
          'r-o','buttondownfcn',@bdfcn,...
          'tag','line');
          axis image
        hold off
        scene_data.handle.im_axis = image_1_handle;
        scene_data.handle.p = p_handle;
        % Create line
        annotation1 = annotation(main_handle,'line',[0.0 1.0],[0.19 0.19]);
     
        % TMP
        add_line_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.78 0.01 0.20 0.035],...
            'string','Cancel',...
            'callback',@cancel_param_obj,...
            'tag','add_line');
     
        % Save scene pushbutton
        add_line_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.78 0.055 0.20 0.035],...
            'string','Save',...
            'callback',@save_scene,...
            'tag','add_line');
        % Undo boutton
        add_line_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.78 0.10 0.20 0.035],...
            'string','Undo',...
            'callback',@scene_undo,...
            'tag','add_line');
     
        % Add Object pushbutton
        add_line_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.78 0.145 0.20 0.035],...
            'string','Add Object',...
            'callback',@add_object,...
            'tag','add_line');
     
        %
        % \TODO add settings Objects
        %
        gui_data=guihandles(gcf);
        guidata(gcf,gui_data);
      end
     
     
      % Add Object Function 
      function add_object(obj,event)
        global scene_data;
     
        sprint('add new object to scene');
        x = get(scene_data.handle.p,'xdata');
        %x
        y = get(scene_data.handle.p,'ydata');
        %y
        scene_data.currentObj.points = [y' x'];
        %scene_data.currentObj.points
        %----------------------------------
        % Object Prameters Figure
        figure('units','pixels',...
            'position',[250 360 300 100],...
            'color',[1.0 1.0 1.0],...
            'numbertitle','off',...
            'MenuBar','none',...
            'name','Set Object Prameters',...
            'tag','init');
        uicontrol('style','edit',...
            'position',[80 55 140 25],...
            'string','class / parameter',...
            'tag','param_obj');
        init_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.2 0.1 0.3 0.25],...
            'string','Ok',...
            'callback',@set_param_obj,...
            'tag','set_param_obj');
        init_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.55 0.1 0.3 0.25],...
            'string','Cancel',...
            'callback',@cancel_param_obj,...
            'tag','set_param_obj');
        gui_data=guihandles(gcf);
        guidata(gcf,gui_data);
      end
     
      % Set Object Parameter Function 
      function set_param_obj(obj,event)
        global scene_data;
        gui_data = guidata(gcf);
        h = str2num(get(gui_data.param_obj,'string'));
        if isempty(h)
           h = 10.0;
        end
        scene_data.currentObj.params = h;
        close gcf
        store_object();
      end
     
      % Cancel Object Parameter Function 
      function cancel_param_obj(obj,event)
        global scene_data;
        gui_data = guidata(gcf);
        close gcf
      end
     
      % Add Object to Scene Function 
      function store_object()
        global scene_data;
        sprint('storing object ...');
        gui_data = guidata(gcf);
        Sx = round(scene_data.currentObj.points(1,1));
        Sy = round(scene_data.currentObj.points(1,2));
        Ex = round(scene_data.currentObj.points(2,1));
        Ey = round(scene_data.currentObj.points(2,2));
        Coords = bresenham(Sx,Sy,Ex,Ey);
        scene_data.matrix(sub2ind(size(scene_data.matrix),Coords(:,1),Coords(:,2))) ...
            = scene_data.currentObj.params;
        scene_data.objects = [scene_data.objects scene_data.currentObj];
        set(gui_data.scene,'CDATA',scene_data.matrix);
        guidata(gcf,gui_data)
      end
     
      % Save Scene Function 
      function save_scene(obj,event)
        global scene_data;
     
        sprint('save scene to .mat file');
        %scene_data.currentObj.points
        %----------------------------------
        % Object Prameters Figure
        figure('units','pixels',...
            'position',[250 360 300 100],...
            'color',[1.0 1.0 1.0],...
            'numbertitle','off',...
            'MenuBar','none',...
            'name','Save Scene',...
            'tag','init');
        uicontrol('style','edit',...
            'position',[80 55 140 25],...
            'string','file name',...
            'tag','file_name');
        init_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.2 0.1 0.3 0.25],...
            'string','Ok',...
            'callback',@store_scene,...
            'tag','set_param_obj');
        init_boutton = uicontrol('style','pushbutton',...
            'units','normalized',...
            'position',[0.55 0.1 0.3 0.25],...
            'string','Cancel',...
            'callback',@cancel_param_obj,...
            'tag','set_param_obj');
        gui_data=guihandles(gcf);
        guidata(gcf,gui_data);
      end
     
      function store_scene(obj,event)
        global scene_data;
        gui_data = guidata(gcf);
     
        fname = get(gui_data.file_name,'string');
        if isempty(fname)
           fname = 'scene_00';
        end
        M = scene_data.matrix;
        save(fname, 'M')
        close gcf
      end
     
      function scene_undo(obj,event)
        global scene_data;
        gui_data = guidata(gcf);
     
        nbObj = length(scene_data.objects);
        if nbObj > 0
           tmpObj = scene_data.objects(end);
           Sx = round(tmpObj.points(1,1));
           Sy = round(tmpObj.points(1,2));
           Ex = round(tmpObj.points(2,1));
           Ey = round(tmpObj.points(2,2));
           Coords = bresenham(Sx,Sy,Ex,Ey);
           scene_data.matrix(sub2ind(size(scene_data.matrix),Coords(:,1),Coords(:,2))) ...
               = 0;
           scene_data.objects = scene_data.objects(1:end-1);
           set(gui_data.scene,'CDATA',scene_data.matrix);
           guidata(gcf,gui_data)
        end
      end
     
      % --------- Mouse Manager ------------
      % Thank you Dut :)
      function bdfcn(obj,event)
        set(gcbf,'WindowButtonMotionFcn',@wbmfcn);      
      end
     
      function wbmfcn(obj,event)
        global scene_data;
        x = get(scene_data.handle.p,'xdata');
        y = get(scene_data.handle.p,'ydata');
        cp = get(gca,'currentpoint');
        cp = cp(1,1:2);
     
        d=sqrt((x-cp(1)).^2+(y-cp(2)).^2);
        [unused,idx]=min(d);
     
        x(idx)=cp(1);
        y(idx)=cp(2);     
        set(scene_data.handle.p,'xdata',x,'ydata',y) 
      end
     
      function wbufcn(obj,event)
        set(gcbf,'WindowButtonMotionFcn',[]);
      end
      %------------- End Mouse manager -------
     
      function sprint(text_string)
      % Special print function
        if nargin < 1 
          fprintf('>> @ ');
          return
        end
     
        global GUI_DEBUG
        if GUI_DEBUG
          fprintf('>> @ ');
          fprintf(text_string);
          fprintf('\n');
        end
      end
     
    %
    %% -------------------------------------------------------
    %% INDEPENDENT FUNCTION
    %
    function [Coords] = bresenham(Sx,Sy,Ex,Ey)
    % function [Coords]=brlinexya(Sx,Sy,Ex,Ey)
    % Bresenham line algorithm.
    % Sx, Sy : starts point
    % Ex, Ey : end points
    % Coords : ordered list of x,y coords.
    % 
     
    DEBUG = false;
     
    if nargin < 4,
        % Demo Mode
        DEBUG = true;
        pxl = 50;
        mymat = zeros(pxl,pxl);
        if DEBUG 
            disp('This is a demo.')
        end
     
     
        imagesc(mymat); axis image;
        if DEBUG
            disp('Click two points on the image.')
        end
        coordinates = ginput(2);
        coordinates
        Sx = round(coordinates(1,2));
        Sy = round(coordinates(1,1));
        Ex = round(coordinates(2,2));
        Ey = round(coordinates(2,1));
    end
     
    	Dx = Ex - Sx;
    	Dy = Ey - Sy;
    	CoordsX = zeros(2 .* ceil(abs(Dx)+abs(Dy)),1);
    	CoordsY = zeros(2 .* ceil(abs(Dx)+abs(Dy)),1);
        iCoords=0;
    	if(abs(Dy) <= abs(Dx))
    		if(Ey >= Sy)
    			if(Ex >= Sx)
    				D = 2*Dy - Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X < Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			else % Ex < Sx
    				D = -2*Dy - Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X > Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			end
    		else % Ey < Sy
    			if(Ex >= Sx)
    				D = 2*Dy + Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X < Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			else % Ex < Sx
    				D = -2*Dy + Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sx;
                    CoordsY(iCoords) = Sy;
    				while(X > Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = X;
                        CoordsY(iCoords) = Y;
    				end
    			end
    		end
    	else % abs(Dy) > abs(Dx) 
    		Tmp = Ex;
    		Ex = Ey;
    		Ey = Tmp;
    		Tmp = Sx;
    		Sx = Sy;
    		Sy = Tmp;
    		Dx = Ex - Sx;
    		Dy = Ey - Sy;
    		if(Ey >= Sy)
    			if(Ex >= Sx)
    				D = 2*Dy - Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X < Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			else % Ex < Sx
    				D = -2*Dy - Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy - Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X > Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y + 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			end
    		else % Ey < Sy
    			if(Ex >= Sx)
    				D = 2*Dy + Dx;
    				IncH = 2*Dy;
    				IncD = 2*(Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X < Ex)
    					if(D >= 0)
    						D = D + IncH;
    						X = X + 1;
    					else
    						D = D + IncD;
    						X = X + 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			else % Ex < Sx
    				D = -2*Dy + Dx;
    				IncH = -2*Dy;
    				IncD = 2*(-Dy + Dx);
    				X = Sx;
    				Y = Sy;
                    iCoords = iCoords + 1;
                    CoordsX(iCoords) = Sy;
                    CoordsY(iCoords) = Sx;
    				while(X > Ex)
    					if(D <= 0)
    						D = D + IncH;
    						X = X - 1;
    					else
    						D = D + IncD;
    						X = X - 1;
    						Y = Y - 1;
                        end
                        iCoords = iCoords + 1;
                        CoordsX(iCoords) = Y;
                        CoordsY(iCoords) = X;
    				end
    			end
    		end
        end
     
      Coords = [CoordsX(1:iCoords),CoordsY(1:iCoords)];
     
      if DEBUG
       mymat(sub2ind(size(mymat),Coords(:,1),Coords(:,2))) = 1;
       imagesc(mymat);
      end
    end % End bresenham function
     
    end
    Si cela peut aider quelqu'un.

    ++ et encore merci

  5. #5
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Vu que tu utilises des fonctions imbriquées, tu n'as pas besoin de définir de variables globales => commente toutes les lignes commençant par GLOBAL.

    Ensuite, la taille et la position de ton interface sont données en unité Pixels.
    Si un utilisateur a un affichage avec une résolution plus faible que la tienne (comme moi ), il ne verra pas les interfaces => utilises les unité Normalized pour positionner les objets Figure. Ou, solution rapide, utilise la fonction MOVEGUI :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    %------------------------------------------------------------
    % @ Intialisation Figure
     
    init_handle = figure('units','pixels',...
        'position',[400 850 400 120],...
        'color',[1.0 1.0 1.0],...
        'numbertitle','off',...
        'MenuBar','none',...
        'name','Scene Generation Program : Intialisation',...
        'tag','init');
     
    movegui(init_handle,'center')
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
        % Main Figure
        main_handle = figure('units','pixels',...
            'position',[200 350 800 600],...
            'WindowButtonMotionFcn',[],...
            'WindowButtonupfcn',@wbufcn,...
            'numbertitle','off',...
            'name','Scene Generation Program : Main',...
            'tag','interface');
        movegui(main_handle,'center')
    Enlève aussi le CLEAR ALL au début de la fonction. Celui-ci ne sert à rien car il n'y a pas d'argument d'entrée pour la fonction, donc aucune au début de l'exécution du code.

  6. #6
    Membre émérite

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2007
    Messages
    979
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2007
    Messages : 979
    Par défaut
    C'est fait .

    Pour global, j'étais parti sur un truc sur plusieurs fichiers au debut. La c'est corrigé.

    Trés bonne idée pour le centrage des figures . Je les plaçaient un peu en hauteur pour voir l'affichage dans la fenêtre de commande.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Déplacer les slicers/segments dans une autre feuille
    Par pedro1090 dans le forum Excel
    Réponses: 4
    Dernier message: 13/09/2011, 10h17
  2. segment vide comment les enlever (je debute en xml)
    Par cocotina dans le forum XML/XSL et SOAP
    Réponses: 2
    Dernier message: 03/10/2005, 11h23
  3. [XLS] De deux listes déplacer les nouvelles entrées
    Par Henrique92 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 20/09/2005, 22h12
  4. Réponses: 3
    Dernier message: 15/04/2004, 08h44
  5. Tracer une ligne droite sans les interruptions
    Par Stef784ever dans le forum x86 16-bits
    Réponses: 4
    Dernier message: 25/11/2002, 01h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo