MATLAB Drawing Tips

This MATLAB script demonstrates how to visualize and customize 3D geometrical elements such as cuboids, cylinders, and arrows, often used to represent components in engineering setups like sensor assemblies.

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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
%% Demo code

close all

clear

% ==============================
% Sketch of Riblet and Sensor Setup
% ==============================
numElement = 290/3.1;

scaleProp = 0.58;
tileDimen1 = 430*scaleProp;
tileDimen2 = 290*scaleProp;
straightRibletLength = 0;%240*scaleProp;

ratioLW = tileDimen1/tileDimen2;
blockLength = tileDimen2; % Length of the riblet block
alpha = 20; % Riblet angle in degrees
numEle = round(numElement*scaleProp); % Number of elements in riblet array
intervalL = blockLength / (numEle - 1); % Distance between elements



rotatingframeL = 90*scaleProp;
rotatingframeW = 30*scaleProp;
diameterShaft = 6*scaleProp;
lengthShaft = 60*scaleProp;
motorSquare = 40*scaleProp;
motorHeight = 20*scaleProp;

% ==============================
%
% ==============================
% Wall-normal, streamwise, and position adjustments |
iZLoc = 1; zz = 0.5;% |
iStep = 25; % Select streamwise location (1 to 6) |
movingX = intervalL / tand(alpha) * (iStep - 1);% |
yLocation = blockLength / 2; % Set y location for drawing |
% % prop = width
%
% 3.2/intervalL;
% movingX*prop
% ==============================
%
% ==============================

% Define rotation angle in radians
theta1 = -[-40:5.4:30]; % 45 degrees, for example
for iRot = 1:length(theta1)

stainlessColor = [180 189 199]/255;
aluminaColor = [175 86 67]/255;

aluminaColor = [1 0.766 0.336];
aluColor = [132 135 137]/255;
sliverColor = [192 192 192]/255;
copperColor = [0.955, 0.637, 0.538];

theta = theta1(iRot);
centrePoint = [straightRibletLength + movingX, yLocation, zz];

hFig = figure;ax(1) = axes;hold(ax(1),'on');
set(ax,'xlim',[0 blockLength*2.2]...
,'ylim',[0*blockLength blockLength]...
,'zLim',[0 blockLength*1]...
,'xtick',[],'ytick',[],'ztick',[])
axis equal;
view(3)
material metal
camlight('headLight');
lighting phong


% ==============================
% draw a probe
% ==============================


scaleProp2 = 1;
hotwireLength = 0.5*scaleProp2;
pronglength1 = 5*scaleProp2;
pronglength2 = 8*scaleProp2;
ceramicLengthx = 10*scaleProp2;
supportLengthx1 = 3*scaleProp2;
supportLengthx2 = 40*scaleProp2;



probeHalf1 = [centrePoint(1), centrePoint(1); ...
centrePoint(2), centrePoint(2) - hotwireLength/2; ...
centrePoint(3), centrePoint(3)];

stubHalf1 = [probeHalf1(1,2) , probeHalf1(1,2);
...
probeHalf1(2,2), probeHalf1(2,2) - hotwireLength/2;
...
centrePoint(3), centrePoint(3);
]; % point to point

prongHalf = [stubHalf1(1,end), stubHalf1(1,end) + pronglength1;...
...
stubHalf1(2,end), stubHalf1(2,end);...
...
stubHalf1(3,end), stubHalf1(3,end)];


prongHalfSec = [prongHalf(1,end), prongHalf(1,end) + pronglength2*cosd(45);...
...
prongHalf(2,end), probeHalf1(2,1) - 1.5*hotwireLength;...
...
prongHalf(3,end), prongHalf(3,end) + pronglength2*cosd(45)];

% Calculate mirrored points for y = blockLength / 2
mirrorY = yLocation;

probeHalf1_mirror = [probeHalf1(1,:); mirrorY + (mirrorY - probeHalf1(2,:)); probeHalf1(3,:)];
stubHalf1_mirror = [stubHalf1(1,:); mirrorY + (mirrorY - stubHalf1(2,:)); stubHalf1(3,:)];
prongHalf_mirror = [prongHalf(1,:); mirrorY + (mirrorY - prongHalf(2,:)); prongHalf(3,:)];
prongHalfSec_mirror = [prongHalfSec(1,:); mirrorY + (mirrorY - prongHalfSec(2,:)); prongHalfSec(3,:)];


% Define ceramic tube relative to prong
% ceremicLengthx = 0.12 * blockLength;

ceramicTube = [prongHalfSec(1,end), prongHalfSec(1,end) + ceramicLengthx ;
...
centrePoint(2), centrePoint(2);
...
prongHalfSec(3,end), prongHalfSec(3,end) + ceramicLengthx * tand(15)];

% define probe support

probeSupport1 = [ceramicTube(1,2), ceramicTube(1,2) + supportLengthx1...
;
...
ceramicTube(2,1), ceramicTube(2,2) ...
;
...
ceramicTube(3,2), ceramicTube(3,2) + supportLengthx1*tand(15) ...
;
];

probeSupport2 = [probeSupport1(1,2), probeSupport1(1,2) + supportLengthx2...
;
...
probeSupport1(2,1), probeSupport1(2,2) ...
;
...
probeSupport1(3,2), probeSupport1(3,2) + supportLengthx2*tand(15) ...
;
];

% Define the rotation matrix around the y-axis
R_y = [cos(theta), 0, sin(theta);
0, 1, 0;
-sin(theta), 0, cos(theta)];
% Define the rotation matrix around the z-axis
R_z = [cosd(theta), -sind(theta), 0;
sind(theta), cosd(theta), 0;
0, 0, 1];

% Define the rotation line
rotation_point = centrePoint';% [3/2 * blockLength; blockLength / 2; 0]; % [x0; y0; z0]
% Rotate each part of the sensor
probeHalf1_rotated = rotate_around_line(probeHalf1, R_z, rotation_point);
probeHalf1_mirror_rotated = rotate_around_line(probeHalf1_mirror, R_z, rotation_point);

stubHalf1_rotated = rotate_around_line(stubHalf1, R_z, rotation_point);
stubHalf1_mirror_rotated = rotate_around_line(stubHalf1_mirror, R_z, rotation_point);

prongHalf_rotated = rotate_around_line(prongHalf, R_z, rotation_point);
prongHalf_mirror_rotated = rotate_around_line(prongHalf_mirror, R_z, rotation_point);

prongHalfSec_rotated = rotate_around_line(prongHalfSec, R_z, rotation_point);
prongHalfSec_mirror_rotated = rotate_around_line(prongHalfSec_mirror, R_z, rotation_point);

ceramicTube_rotated = rotate_around_line(ceramicTube, R_z, rotation_point);

probeSupport1_rotated = rotate_around_line(probeSupport1,R_z,rotation_point);

probeSupport2_rotated = rotate_around_line(probeSupport2,R_z,rotation_point);


diameter = 0.01*scaleProp2;
[cyX, cyY, cyZ] = plotCylinder(probeHalf1_rotated(:,1)', probeHalf1_rotated(:,2)', diameter);
surf(cyX, cyY, cyZ, 'FaceColor', 'r', 'EdgeColor', 'none');
[cyX, cyY, cyZ] = plotCylinder(probeHalf1_mirror_rotated(:,1)', probeHalf1_mirror_rotated(:,2)', diameter);
surf(cyX, cyY, cyZ, 'FaceColor', 'r', 'EdgeColor', 'none');

diameter = .1*scaleProp2;
[cyX, cyY, cyZ] = plotCylinder(stubHalf1_rotated(:,1)', stubHalf1_rotated(:,2)', diameter);
surf(cyX, cyY, cyZ, 'FaceColor', sliverColor, 'EdgeColor', 'none');
[cyX, cyY, cyZ] = plotCylinder(stubHalf1_mirror_rotated(:,1)', stubHalf1_mirror_rotated(:,2)', diameter);
surf(cyX, cyY, cyZ, 'FaceColor', sliverColor, 'EdgeColor', 'none');


diameter1 = 0.1*scaleProp2;
diameter2 = 0.6*scaleProp2;
[cyX, cyY, cyZ] = plotTruncatedCone(prongHalf_rotated(:,1)', prongHalf_rotated(:,2)', diameter1, diameter2);
surf(cyX, cyY, cyZ, 'FaceColor', stainlessColor, 'EdgeColor', 'none');
[cyX, cyY, cyZ] = plotTruncatedCone(prongHalf_mirror_rotated(:,1)', prongHalf_mirror_rotated(:,2)', diameter1, diameter2);
surf(cyX, cyY, cyZ, 'FaceColor', stainlessColor, 'EdgeColor', 'none');


diameter1 = 0.6*scaleProp2;
normal1 = [1 0 0];
diameter2 = 0.6*scaleProp2;
normal2 = [cosd(15) 0 sind(15)];
segments = 40;
[cyX, cyY, cyZ] = plotCustomOrientedCylinder(prongHalfSec_rotated(:,1)', diameter1, normal1...
, prongHalfSec_rotated(:,2)', diameter2, normal2, segments);
surf(cyX, cyY, cyZ, 'FaceColor', stainlessColor, 'EdgeColor', 'none');
[cyX, cyY, cyZ] = plotCustomOrientedCylinder(prongHalfSec_mirror_rotated(:,1)', diameter1, normal1...
, prongHalfSec_mirror_rotated(:,2)', diameter2, normal2, segments);
surf(cyX, cyY, cyZ, 'FaceColor', stainlessColor, 'EdgeColor', 'none');


diameter = 2.3*scaleProp2;
[cyX, cyY, cyZ] = plotCylinder(ceramicTube_rotated(:,1)', ceramicTube_rotated(:,2)', diameter);
surf(cyX, cyY, cyZ, 'FaceColor', aluminaColor, 'EdgeColor', 'none','FaceAlpha',0.96);



diameter1 = 2.4*scaleProp2;
diameter2 = 4.5*scaleProp2;
[cyX, cyY, cyZ] = plotTruncatedCone(probeSupport1_rotated(:,1)', probeSupport1_rotated(:,2)', diameter1, diameter2);
surf(cyX, cyY, cyZ, 'FaceColor', aluColor, 'EdgeColor', 'none');

diameter1 = 4.5*scaleProp2;
diameter2 = 4.5*scaleProp2;
[cyX, cyY, cyZ] = plotTruncatedCone(probeSupport2_rotated(:,1)', probeSupport2_rotated(:,2)', diameter1, diameter2);
surf(cyX, cyY, cyZ, 'FaceColor', aluColor, 'EdgeColor', 'none');




% print(hFig,'-depsc2',['probe' num2str(iRot)])
%
% print(hFig,'-dtiff','-r500',['probe' num2str(iRot)])

% ==============================
% draw rotating frame
% ==============================

% define holder 3D - Printing
holderPrinting = [1.04*(probeSupport2(1,2) + probeSupport2(1,1))/2, 1.0*(probeSupport2(1,2) + probeSupport2(1,1))/2; ...
(probeSupport2(2,2) + probeSupport2(2,1))/2, (probeSupport2(2,2) + probeSupport2(2,1))/2; ...
1.07*(probeSupport2(3,2) + probeSupport2(3,1))/2, 1.07*(probeSupport2(3,2) + probeSupport2(3,1))/2; ...
];

% rotating frame
rotatingFrame1 = [(holderPrinting(1,1)+holderPrinting(1,2))/2 + 0.03*blockLength , (holderPrinting(1,1)+holderPrinting(1,2))/2 - 0.03*blockLength; ...
holderPrinting(2,2), holderPrinting(2,2); ...
holderPrinting(3,2) + 0.18*blockLength + 0.025*blockLength, holderPrinting(3,2) + 0.18*blockLength + 0.025*blockLength; ...
];

% rotating frame
rotatingFrame2 = [rotatingFrame1(1,2) + 0.06*blockLength, rotatingFrame1(1,2) - abs(rotatingFrame1(1,2) - centrePoint(1)); ...
rotatingFrame1(2,2), rotatingFrame1(2,2) ; ...
rotatingFrame1(3,2) + 0.1825*blockLength , rotatingFrame1(3,2) + 0.1825*blockLength; ...
];


% define motor shaft
motorShaft = [centrePoint(1), centrePoint(1) ;
centrePoint(2), centrePoint(2) ;
rotatingFrame2(3,2) - 0.03*blockLength ...
rotatingFrame2(3,2) + lengthShaft;
];

% define motor
motor = [motorShaft(1,2), motorShaft(1,2) ; ...
motorShaft(2,2), motorShaft(2,2) ; ...
motorShaft(3,2), motorShaft(3,2) + 0.07*blockLength ...
];




holderPrinting_rotated = rotate_around_line(holderPrinting,R_z,rotation_point);
rotatingFrame1_rotated = rotate_around_line(rotatingFrame1,R_z,rotation_point);
rotatingFrame2_rotated = rotate_around_line(rotatingFrame2,R_z,rotation_point);

motorShaft_rotated = motorShaft;%rotate_around_line(motorShaft,R_z,rotation_point);
motor_rotated = motor;% rotate_around_line(motor,R_z,rotation_point);



width1 = 0.05*blockLength;
length1 = 0.06*blockLength;

dirVec1 = probeHalf1_rotated(:,1)' - probeHalf1_rotated(:,2)';
dirVec2 = [0,0,1];

normal1 = cross(dirVec1,dirVec2);
refDir1 = dirVec1;

width2 = 0.05*blockLength;
length2 = 0.06*blockLength;
normal2 = cross(dirVec1,dirVec2);
refDir2 = dirVec1;

[X_interp, Y_interp, Z_interp, faces] = ...
plotCustomOrientedCuboid(holderPrinting_rotated(:,1)', width1, length1, normal1, refDir1 ...
, holderPrinting_rotated(:,2)', width2, length2, normal2, refDir2);
patch('Vertices', [X_interp(:), Y_interp(:), Z_interp(:)], 'Faces', faces, ...
'FaceColor', copperColor, 'FaceAlpha', 1, 'EdgeColor', 'none');


width1 = (0.36)*blockLength;
length1 = 0.01*blockLength;
dirVec1 = probeHalf1_rotated(:,1)' - probeHalf1_rotated(:,2)';
dirVec2 = [0,0,1];

normal1 = cross(dirVec1,dirVec2);%[1 0 0];
refDir1 = dirVec2;

width2 = (0.36)*blockLength;
length2 = 0.01*blockLength;
dirVec1 = probeHalf1_rotated(:,1)' - probeHalf1_rotated(:,2)';
dirVec2 = [0,0,1];

normal2 = cross(dirVec1,dirVec2);%[1 0 0];
refDir2 = dirVec2;

[X_interp, Y_interp, Z_interp, faces] = ...
plotCustomOrientedCuboid(rotatingFrame1_rotated(:,1)', width1, length1, normal1, refDir1 ...
, rotatingFrame1_rotated(:,2)', width2, length2, normal2, refDir2);
patch('Vertices', [X_interp(:), Y_interp(:), Z_interp(:)], 'Faces', faces, ...
'FaceColor', stainlessColor, 'FaceAlpha', 1, 'EdgeColor', 'none');



width1 = 0.05*blockLength;
length1 = 0.01*blockLength;

dirVec1 = probeHalf1_rotated(:,1)' - probeHalf1_rotated(:,2)';
dirVec2 = [0,0,1];

normal1 = cross(dirVec1,dirVec2);%[1 0 0];
refDir1 = dirVec2;

width2 = 0.05*blockLength;
length2 = 0.01*blockLength;

dirVec1 = probeHalf1_rotated(:,1)' - probeHalf1_rotated(:,2)';
dirVec2 = [0,0,1];

normal2 = cross(dirVec1,dirVec2);%[1 0 0];
refDir2 = dirVec2;

[X_interp, Y_interp, Z_interp, faces] = ...
plotCustomOrientedCuboid(rotatingFrame2_rotated(:,1)', width1, length1, normal1, refDir1 ...
, rotatingFrame2_rotated(:,2)', width2, length2, normal2, refDir2);
patch('Vertices', [X_interp(:), Y_interp(:), Z_interp(:)], 'Faces', faces, ...
'FaceColor', stainlessColor, 'FaceAlpha', 1, 'EdgeColor', 'none');



diameter = diameterShaft;
[cyX, cyY, cyZ] = plotCylinder(motorShaft_rotated(:,1)', motorShaft_rotated(:,2)', diameter);
surf(cyX, cyY, cyZ, 'FaceColor', aluColor, 'EdgeColor', 'none','FaceAlpha',0.99);


width1 = motorSquare;
length1 = motorSquare;

dirVec1 = probeHalf1(:,1)' - probeHalf1(:,2)';
dirVec2 = [0,0,1];
normal1 = [0 0 1];
refDir1 = [0 1 0];

width2 = motorSquare;
length2 = motorSquare;
dirVec1 = probeHalf1(:,1)' - probeHalf1(:,2)';
dirVec2 = [0,0,1];

normal2 = [0 0 1];
refDir2 = [0 1 0];

[X_interp, Y_interp, Z_interp, faces] = ...
plotCustomOrientedCuboid(motor_rotated(:,1)', width1, length1, normal1, refDir1 ...
, motor_rotated(:,2)', width2, length2, normal2, refDir2);
patch('Vertices', [X_interp(:), Y_interp(:), Z_interp(:)], 'Faces', faces, ...
'FaceColor', 'k', 'FaceAlpha', 0.8, 'EdgeColor', 'none');

plot3(ax,[centrePoint(1) centrePoint(1)],[centrePoint(2) centrePoint(2)],[10 150],'k--','LineWidth',1.3)



pause(0.1);
plot3([straightRibletLength straightRibletLength],[0 -blockLength*0.1],[0 0],'LineStyle','-','LineWidth',1.2,'Color','b')
plot3([centrePoint(1) centrePoint(1)],[0 -blockLength*0.1],[0 0],'LineStyle','-','LineWidth',1.2,'Color','b')
startPoint = [straightRibletLength, -blockLength*0.05, 0];
endPoint = [centrePoint(1), -blockLength*0.05, 0];
arrowSizeFactor = 0.18;
ax = gca;
color = 'b';
lineWidth = 1.2;
arrowAngleDeg = 25;
drawDoubleArrowWithCustomArrows(startPoint, endPoint, color, lineWidth, arrowSizeFactor, arrowAngleDeg, ax)
textLabel = [num2str(abs((centrePoint(1) - straightRibletLength)*tand(alpha)/sind(alpha)),'%.1f')];
textPosition = (startPoint + endPoint) / 2 + [0, -blockLength*0.1, 0.5]; % Position text above the line

text(ax,textPosition(1), textPosition(2), textPosition(3), textLabel, 'FontSize', 11, 'Color', 'r', 'FontWeight', 'bold', 'HorizontalAlignment', 'center');

pause(0.1)

plot3([motor(1,2)-0.1*blockLength motor(1,2) - 0.3*blockLength],[motor(2,2) motor(2,2)],[motor(3,1) motor(3,1)],'LineStyle','-','LineWidth',1.2,'Color','b')
plot3([motor(1,2)-0.1*blockLength motor(1,2) - 0.3*blockLength],[motor(2,2) motor(2,2)],[0 0],'LineStyle','-','LineWidth',1.2,'Color','b')
startPoint = [motor(1,2) - 0.2*blockLength, motor(2,2), motor(3,1)];
endPoint = [motor(1,2) - 0.2*blockLength, motor(2,2), 0];
arrowSizeFactor = 0.1;
ax = gca;
color = 'b';
lineWidth = 1.2;
arrowAngleDeg = 25;
drawDoubleArrowWithCustomArrows(startPoint, endPoint, color, lineWidth, arrowSizeFactor, arrowAngleDeg, ax)
textLabel = [num2str(abs((motor(3,1)))/scaleProp,'%.1f')];
textPosition = (startPoint + endPoint) / 2 + [-blockLength*0.2, 0, 0.5]; % Position text above the line
text(ax,textPosition(1), textPosition(2), textPosition(3), textLabel, 'FontSize', 11, 'Color', 'r', 'FontWeight', 'bold', 'HorizontalAlignment', 'center');


pause(0.1)

plot3([motor(1,2) - 1.0*motorSquare motor(1,2) - 0.5*motorSquare],[motor(2,2)-0.5*motorSquare motor(2,2)-0.5*motorSquare],[motor(3,2) motor(3,2)],'LineStyle','-','LineWidth',1.2,'Color','b')
plot3([motor(1,2) - 1.0*motorSquare motor(1,2) - 0.5*motorSquare],[motor(2,2)+0.5*motorSquare motor(2,2)+0.5*motorSquare],[motor(3,2) motor(3,2)],'LineStyle','-','LineWidth',1.2,'Color','b')
startPoint = [motor(1,2) - 0.75*motorSquare, motor(2,2)-0.5*motorSquare, motor(3,2)];
endPoint = [motor(1,2) - 0.75*motorSquare, motor(2,2)+0.5*motorSquare, motor(3,2)];
arrowSizeFactor = 0.3;
ax = gca;
color = 'b';
lineWidth = 1.2;
arrowAngleDeg = 25;
drawDoubleArrowWithCustomArrows(startPoint, endPoint, color, lineWidth, arrowSizeFactor, arrowAngleDeg, ax)
textLabel = [num2str(abs(motorSquare)/scaleProp,'%.1f')];

textPosition = (startPoint + endPoint) / 2 + [0, blockLength*0.2, 0.5]; % Position text above the line
text(ax,textPosition(1), textPosition(2), textPosition(3), textLabel, 'FontSize', 11, 'Color', 'r', 'FontWeight', 'bold', 'HorizontalAlignment', 'center');

pause(0.1)

plot3([rotatingFrame1_rotated(1,1)...
rotatingFrame1_rotated(1,1) + 0.5*rotatingframeW]...
,[rotatingFrame1_rotated(2,1) ...
rotatingFrame1_rotated(2,1)] ...
,[rotatingFrame1_rotated(3,1) - 0.5*rotatingframeL ...
rotatingFrame1_rotated(3,1) - 0.5*rotatingframeL]...
,'LineStyle','-','LineWidth',1.2,'Color','b')

plot3([rotatingFrame1_rotated(1,1)...
rotatingFrame1_rotated(1,1) + 0.5*rotatingframeW]...
,[rotatingFrame1_rotated(2,1) ...
rotatingFrame1_rotated(2,1)] ...
,[rotatingFrame1_rotated(3,1) + 0.5*rotatingframeL ...
rotatingFrame1_rotated(3,1) + 0.5*rotatingframeL]...
,'LineStyle','-','LineWidth',1.2,'Color','b')

startPoint = [rotatingFrame1_rotated(1,1) + 0.25*rotatingframeW, rotatingFrame1_rotated(2,1), rotatingFrame1_rotated(3,1) - 0.5*rotatingframeL];
endPoint = [rotatingFrame1_rotated(1,1) + 0.25*rotatingframeW, rotatingFrame1_rotated(2,1), rotatingFrame1_rotated(3,1) + 0.5*rotatingframeL];
arrowSizeFactor = 0.3;
ax = gca;
color = 'b';
lineWidth = 1.2;
arrowAngleDeg = 25;
drawDoubleArrowWithCustomArrows(startPoint, endPoint, color, lineWidth, arrowSizeFactor, arrowAngleDeg, ax)
textLabel = [num2str(abs(rotatingframeL)/scaleProp,'%.1f')];

textPosition = (startPoint + endPoint) / 2 + [blockLength*0.2, 0, 0.5]; % Position text above the line
text(ax,textPosition(1), textPosition(2), textPosition(3), textLabel, 'FontSize', 11, 'Color', 'r', 'FontWeight', 'bold', 'HorizontalAlignment', 'center');

pause(0.1)

plot3([rotatingFrame2_rotated(1,1)...
rotatingFrame2_rotated(1,1)]...
,[rotatingFrame2_rotated(2,1) ...
rotatingFrame2_rotated(2,1) - 1*rotatingframeW] ...
,[rotatingFrame2_rotated(3,1) ...
rotatingFrame2_rotated(3,1)]...
,'LineStyle','-','LineWidth',1.2,'Color','b')

plot3([rotatingFrame2_rotated(1,2)...
rotatingFrame2_rotated(1,2)]...
,[rotatingFrame2_rotated(2,1) ...
rotatingFrame2_rotated(2,1) - 1*rotatingframeW] ...
,[rotatingFrame2_rotated(3,1) ...
rotatingFrame2_rotated(3,1)]...
,'LineStyle','-','LineWidth',1.2,'Color','b')

startPoint = [rotatingFrame2_rotated(1,1), rotatingFrame2_rotated(2,1) - 0.5*rotatingframeW , rotatingFrame2_rotated(3,1)];
endPoint = [rotatingFrame2_rotated(1,2), rotatingFrame2_rotated(2,1) - 0.5*rotatingframeW, rotatingFrame2_rotated(3,1)];
arrowSizeFactor = 0.1;
ax = gca;
color = 'b';
lineWidth = 1.2;
arrowAngleDeg = 25;
drawDoubleArrowWithCustomArrows(startPoint, endPoint, color, lineWidth, arrowSizeFactor, arrowAngleDeg, ax)
textLabel = [num2str(abs(rotatingframeL)/scaleProp,'%.1f')];

textPosition = (startPoint + endPoint) / 2 + [-blockLength*0.1, -blockLength*0.2, 3]; % Position text above the line
text(ax,textPosition(1), textPosition(2), textPosition(3), textLabel, 'FontSize', 11, 'Color', 'r', 'FontWeight', 'bold', 'HorizontalAlignment', 'center');




pause(0.5)
set(ax,'xlim',[1.1*tileDimen1/2 blockLength*1.8]...
,'ylim',[0.7*blockLength/3 1.3*2*blockLength/3]...
,'zLim',[0 blockLength*1/2*1.1]...
,'xtick',[],'ytick',[],'ztick',[])

pause(0.2)

set(ax,'xlim',[1.3*tileDimen1/2 blockLength*1.7]...
,'ylim',[blockLength/3 2*blockLength/3]...
,'zLim',[0 blockLength*1/2]...
,'xtick',[],'ytick',[],'ztick',[])


pause(0.2)

set(ax,'xlim',[tileDimen1/2*1.6 blockLength*1.35]...
,'ylim',[2.2*blockLength/5 2.8*blockLength/5]...
,'zLim',[0 blockLength*1/7]...
,'xtick',[],'ytick',[],'ztick',[])


end



%%

function drawDoubleArrowWithCustomArrows(startPoint, endPoint, color, lineWidth, arrowSizeFactor, arrowAngleDeg, ax)
% Draws a 3D line with custom double arrows at each end, scaled to line length
% Inputs:
% startPoint - 1x3 vector [x1, y1, z1] for the line's starting point
% endPoint - 1x3 vector [x2, y2, z2] for the line's ending point
% color - Color of the line and arrows
% lineWidth - Width of the line
% arrowSizeFactor - Fraction of line length for arrowhead size
% arrowAngleDeg - Angle of arrowhead opening in degrees

% Extract coordinates and calculate direction vector and line length
x1 = startPoint(1); y1 = startPoint(2); z1 = startPoint(3);
x2 = endPoint(1); y2 = endPoint(2); z2 = endPoint(3);
dirVec = [x2 - x1, y2 - y1, z2 - z1];
lineLength = norm(dirVec);

% Normalize direction vector and calculate arrowhead size
unitDirVec = dirVec / lineLength;
arrowSize = lineLength * arrowSizeFactor;
arrowAngleRad = deg2rad(arrowAngleDeg);

% Calculate perpendicular vector for the arrow plane
referenceVec = [0, 0, 1];
if abs(dot(referenceVec, unitDirVec)) == 1
referenceVec = [0, 1, 0];
end
arrowPlaneVec = cross(unitDirVec, referenceVec);
arrowPlaneVec = arrowPlaneVec / norm(arrowPlaneVec);

% Plot the main line
plot3(ax, [x1, x2], [y1, y2], [z1, z2], 'Color', color, 'LineWidth', lineWidth);
hold on;

% Draw arrowheads at the start and end points
drawArrowhead(x1, y1, z1, unitDirVec, arrowPlaneVec, arrowSize, arrowAngleRad, color, lineWidth, ax); % Start arrow
drawArrowhead(x2, y2, z2, -unitDirVec, arrowPlaneVec, arrowSize, arrowAngleRad, color, lineWidth, ax); % End arrow
end

function drawArrowhead(x, y, z, unitDirVec, arrowPlaneVec, arrowSize, arrowAngleRad, color, lineWidth, ax)
% Draws an arrowhead at a given point in the specified direction
% The arrowhead points away from the line.

% Arrow base point at the end of the arrow
arrowBase = [x, y, z]; % Base point is the arrow location (end of the line)

% Calculate the two arrowhead endpoints forming the arrow's "V" shape
arrowEnd1 = arrowBase + arrowSize * (cos(arrowAngleRad) * unitDirVec + sin(arrowAngleRad) * arrowPlaneVec);
arrowEnd2 = arrowBase + arrowSize * (cos(arrowAngleRad) * unitDirVec - sin(arrowAngleRad) * arrowPlaneVec);

% Draw each side of the arrowhead using plot3
plot3(ax, [arrowBase(1), arrowEnd1(1)], [arrowBase(2), arrowEnd1(2)], [arrowBase(3), arrowEnd1(3)], 'Color', color, 'LineWidth', lineWidth);
plot3(ax, [arrowBase(1), arrowEnd2(1)], [arrowBase(2), arrowEnd2(2)], [arrowBase(3), arrowEnd2(3)], 'Color', color, 'LineWidth', lineWidth);
end

function rotated_points = rotate_around_line(points, R, rotation_point)
% Translate points to align rotation point with the origin
points_translated = points;
points_translated(1:2, :) = points_translated(1:2, :) - rotation_point(1:2);

% Apply rotation
rotated_translated = R * points_translated;

% Translate back
rotated_points = rotated_translated;
rotated_points(1:2, :) = rotated_points(1:2, :) + rotation_point(1:2);
end

function [cyX, cyY, cyZ] = plotCylinder(point1, point2, diameter)
% plotCylinder: Plots a cylinder between two points in 3D space.
%
% Inputs:
% point1 - Starting point of the cylinder [x1, y1, z1]
% point2 - Ending point of the cylinder [x2, y2, z2]
% diameter - Diameter of the cylinder
% color - Color of the cylinder as a character or RGB triplet
%
% Example usage:
% plotCylinder([0, 0, 0], [1, 1, 1], 0.5, 'r');

% Calculate the cylinder properties
radius = diameter / 2;
numPoints = 20; % Resolution of the cylinder
[cyX, cyY, cyZ] = cylinder(radius, numPoints);

% Vector from point1 to point2
vec = point2 - point1;
len = norm(vec); % Length of the cylinder

% Scale the cylinder height
cyZ = cyZ * len;

% Rotation calculations
theta = atan2(vec(2), vec(1)); % Yaw rotation (around Z-axis)
phi = acos(vec(3) / len); % Pitch rotation (around Y-axis)

% Rotation matrices
Rz = [cos(theta), -sin(theta), 0; sin(theta), cos(theta), 0; 0, 0, 1];
Ry = [cos(phi), 0, sin(phi); 0, 1, 0; -sin(phi), 0, cos(phi)];

% Rotate and translate the cylinder to align with the vector
cylinderPoints = [cyX(:), cyY(:), cyZ(:)]';
rotatedPoints = Rz * Ry * cylinderPoints;

% Reshape back and translate to starting point
cyX = reshape(rotatedPoints(1,:), size(cyX)) + point1(1);
cyY = reshape(rotatedPoints(2,:), size(cyY)) + point1(2);
cyZ = reshape(rotatedPoints(3,:), size(cyZ)) + point1(3);

end

function [cyX, cyY, cyZ] = plotTruncatedCone(point1, point2, diameter1, diameter2)
% plotTruncatedCone: Plots a truncated cone between two points in 3D space.
%
% Inputs:
% point1 - Starting point of the truncated cone [x1, y1, z1]
% point2 - Ending point of the truncated cone [x2, y2, z2]
% diameter1 - Diameter of the base (start) of the truncated cone
% diameter2 - Diameter of the top (end) of the truncated cone
% color - Color of the truncated cone as a character or RGB triplet
%
% Example usage:
% plotTruncatedCone([0, 0, 0], [1, 1, 1], 4, 5, 'g');

% Calculate radii
radius1 = diameter1 / 2;
radius2 = diameter2 / 2;
numPoints = 20; % Resolution of the cone
[cyX, cyY, cyZ] = cylinder([radius1, radius2], numPoints);

% Vector from point1 to point2
vec = point2 - point1;
len = norm(vec); % Length of the cone

% Scale the cone height
cyZ = cyZ * len;

% Rotation calculations
theta = atan2(vec(2), vec(1)); % Yaw rotation (around Z-axis)
phi = acos(vec(3) / len); % Pitch rotation (around Y-axis)

% Rotation matrices
Rz = [cos(theta), -sin(theta), 0; sin(theta), cos(theta), 0; 0, 0, 1];
Ry = [cos(phi), 0, sin(phi); 0, 1, 0; -sin(phi), 0, cos(phi)];

% Rotate and translate the cone to align with the vector
conePoints = [cyX(:), cyY(:), cyZ(:)]';
rotatedPoints = Rz * Ry * conePoints;

% Reshape back and translate to the starting point
cyX = reshape(rotatedPoints(1,:), size(cyX)) + point1(1);
cyY = reshape(rotatedPoints(2,:), size(cyY)) + point1(2);
cyZ = reshape(rotatedPoints(3,:), size(cyZ)) + point1(3);

end

function [X_interp, Y_interp, Z_interp] = plotCustomOrientedCylinder(point1, diameter1, normal1, point2, diameter2, normal2, segments)
% plotCustomOrientedCylinder: Creates a cylinder with custom orientation planes at each end.
%
% Inputs:
% point1 - 1x3 vector specifying the center of the bottom plane [x, y, z]
% diameter1 - Diameter of the bottom plane
% normal1 - 1x3 vector specifying the normal of the bottom plane
% point2 - 1x3 vector specifying the center of the top plane [x, y, z]
% diameter2 - Diameter of the top plane
% normal2 - 1x3 vector specifying the normal of the top plane
% segments - Number of segments around the circumference
% color - Color of the cylinder as a character or RGB triplet

radius1 = diameter1 / 2;
radius2 = diameter2 / 2;

% Normalize the input normal vectors
normal1 = normal1 / norm(normal1);
normal2 = normal2 / norm(normal2);

% Generate a basic cylinder with the specified radii
[X, Y, Z] = cylinder([radius1, radius2], segments);
height = norm(point2 - point1); % Scale height to the distance between points
Z = Z * height;

% Align the bottom plane with normal1
[X1, Y1, Z1] = applyRotation(X(1, :), Y(1, :), Z(1, :), point1, normal1);

% Align the top plane with normal2
[X2, Y2, Z2] = applyRotation(X(2, :), Y(2, :), Z(2, :), point2, normal2);

% Interpolate between the two aligned planes to create a smooth transition
X_interp = [X1; X2];
Y_interp = [Y1; Y2];
Z_interp = [Z1; Z2];

% Plot the custom-oriented cylinder
% surf(X_interp, Y_interp, Z_interp, 'FaceColor', color, 'EdgeColor', 'none');
% axis equal;
end

function [X_rot, Y_rot, Z_rot] = applyRotation(X, Y, Z, center, normal)
% applyRotation: Rotates points to align with a given normal and translates to center.
%
% Inputs:
% X, Y, Z - Coordinates of points to be rotated (relative to origin)
% center - Target center of rotation [x, y, z]
% normal - Target normal for the plane [nx, ny, nz]
%
% Outputs:
% X_rot, Y_rot, Z_rot - Rotated and translated coordinates

% Define the original normal (along the Z-axis)
zAxis = [0, 0, 1];

% Calculate the rotation axis and angle to align zAxis with 'normal'
rotAxis = cross(zAxis, normal);
rotAngle = acos(dot(zAxis, normal) / (norm(zAxis) * norm(normal)));

if norm(rotAxis) > 1e-6
% Normalize the rotation axis
rotAxis = rotAxis / norm(rotAxis);

% Create rotation matrix using Rodrigues' rotation formula
K = [0 -rotAxis(3) rotAxis(2); rotAxis(3) 0 -rotAxis(1); -rotAxis(2) rotAxis(1) 0];
R = eye(3) + sin(rotAngle) * K + (1 - cos(rotAngle)) * (K * K);
else
% If rotation is negligible (i.e., z-axis already aligned with normal)
R = eye(3);
end

% Center points at the origin before rotating
points = [X - mean(X(:)); Y - mean(Y(:)); Z - mean(Z(:))];

% Apply rotation
rotatedPoints = R * points;

% Translate points back to the specified center
X_rot = rotatedPoints(1, :) + center(1);
Y_rot = rotatedPoints(2, :) + center(2);
Z_rot = rotatedPoints(3, :) + center(3);
end

function [X_interp, Y_interp, Z_interp, faces] = plotCustomOrientedCuboid(point1, width1, length1, normal1, refDir1, point2, width2, length2, normal2, refDir2)
% plotCustomOrientedCuboid: Creates a cuboid with custom rectangular cross-sections
% at each end and orientation based on normals.
%
% Inputs:
% point1 - 1x3 vector specifying the center of the bottom face [x, y, z]
% width1 - Width of the bottom rectangular face
% length1 - Length of the bottom rectangular face
% normal1 - 1x3 vector specifying the normal of the bottom face
% refDir1 - 1x3 vector specifying a reference direction for one side of the bottom face
% point2 - 1x3 vector specifying the center of the top face [x, y, z]
% width2 - Width of the top rectangular face
% length2 - Length of the top rectangular face
% normal2 - 1x3 vector specifying the normal of the top face
% refDir2 - 1x3 vector specifying a reference direction for one side of the top face
%
% Outputs:
% X_interp, Y_interp, Z_interp - Coordinates for the faces of the cuboid

% Half-dimensions for rectangular faces
half_width1 = width1 / 2;
half_length1 = length1 / 2;
half_width2 = width2 / 2;
half_length2 = length2 / 2;

% Normalize the input normal and reference direction vectors
normal1 = normal1 / norm(normal1);
normal2 = normal2 / norm(normal2);
refDir1 = refDir1 / norm(refDir1);
refDir2 = refDir2 / norm(refDir2);

% Generate rectangular vertices in the XY-plane for both ends
rect1 = [-half_width1, -half_length1, 0;
half_width1, -half_length1, 0;
half_width1, half_length1, 0;
-half_width1, half_length1, 0];
rect2 = [-half_width2, -half_length2, 0;
half_width2, -half_length2, 0;
half_width2, half_length2, 0;
-half_width2, half_length2, 0];

% Rotate and translate rectangles to their respective positions and orientations
[X1, Y1, Z1] = applyRotationCuboid(rect1(:,1), rect1(:,2), rect1(:,3), point1, normal1, refDir1);
[X2, Y2, Z2] = applyRotationCuboid(rect2(:,1), rect2(:,2), rect2(:,3), point2, normal2, refDir2);

% Combine vertices into X_interp, Y_interp, Z_interp for the full cuboid
X_interp = [X1; X2];
Y_interp = [Y1; Y2];
Z_interp = [Z1; Z2];

% Define the faces of the cuboid using patch, referring to X_interp, Y_interp, Z_interp
faces = [
1, 2, 6, 5; % Side 1
2, 3, 7, 6; % Side 2
3, 4, 8, 7; % Side 3
4, 1, 5, 8; % Side 4
1, 2, 3, 4; % Bottom face
5, 6, 7, 8 % Top face
];

% Plot the cuboid (optional)
% patch('Vertices', [X_interp(:), Y_interp(:), Z_interp(:)], 'Faces', faces, ...
% 'FaceColor', 'b', 'FaceAlpha', 0.5, 'EdgeColor', 'none');
% axis equal;
end

function [X_rot, Y_rot, Z_rot] = applyRotationCuboid(X, Y, Z, center, normal, refDir)
% applyRotationCuboid: Rotates points to align with both the normal and reference direction,
% and translates to the specified center.
%
% Inputs:
% X, Y, Z - Coordinates of points to be rotated (relative to origin)
% center - Target center of rotation [x, y, z]
% normal - Target normal for the plane [nx, ny, nz]
% refDir - Target reference direction for an edge [rx, ry, rz]
%
% Outputs:
% X_rot, Y_rot, Z_rot - Rotated and translated coordinates

% Calculate rotation matrix to align Z-axis with 'normal'
zAxis = [0, 0, 1];
rotAxis = cross(zAxis, normal);
rotAngle = acos(dot(zAxis, normal));

if norm(rotAxis) > 1e-6
rotAxis = rotAxis / norm(rotAxis);
K = [0 -rotAxis(3) rotAxis(2); rotAxis(3) 0 -rotAxis(1); -rotAxis(2) rotAxis(1) 0];
R1 = eye(3) + sin(rotAngle) * K + (1 - cos(rotAngle)) * (K * K);
else
R1 = eye(3);
end

% Rotate points to align with normal
points = [X, Y, Z]';
rotatedPoints = R1 * points;

% Calculate the in-plane rotation to align X-axis with 'refDir'
xAxisInPlane = R1(:,1);
inPlaneAxis = cross(xAxisInPlane, refDir);
inPlaneAngle = acos(dot(xAxisInPlane, refDir));

if norm(inPlaneAxis) > 1e-6
inPlaneAxis = inPlaneAxis / norm(inPlaneAxis);
K_inPlane = [0 -inPlaneAxis(3) inPlaneAxis(2); inPlaneAxis(3) 0 -inPlaneAxis(1); -inPlaneAxis(2) inPlaneAxis(1) 0];
R2 = eye(3) + sin(inPlaneAngle) * K_inPlane + (1 - cos(inPlaneAngle)) * (K_inPlane * K_inPlane);
else
R2 = eye(3);
end

% Apply the in-plane rotation
finalRotatedPoints = R2 * rotatedPoints;

% Translate points to the specified center
X_rot = finalRotatedPoints(1, :)' + center(1);
Y_rot = finalRotatedPoints(2, :)' + center(2);
Z_rot = finalRotatedPoints(3, :)' + center(3);
end







打赏
  • © 2020-2025 Yu Xia
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

Buy me a cup of coffee~

支付宝
微信