I'm trying to draw a rectangle wit random points inside using tikz. My attempt is this:
\documentclass[tikz]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usetikzlibrary{calc}
\pgfmathsetseed{20}
\tikzset{
particle/.style = {draw,circle,inner sep=0,outer sep=0,minimum size=3}
}
\tikzset{
pics/PE/.style
2 args={
code={
\node[
draw,rectangle,
minimum width=80,minimum height=40] (main) at (0,0) {};
\foreach \i in {1,...,#2}
{
\path let \p1 = (main.south west), \p2 = (main.north east) in
node[particle] at
($ (main.south west) + ({random(\x1,\x2)},{random(\y1,\y2)}) $) {};
}
}
}
}
\begin{document}
\begin{tikzpicture}
\draw pic {PE={1}{30}};
\end{tikzpicture}
\end{document}
...but this doesn't work. It seems I cannot use a coordinate inside a call to the random function. Is that so? Any workaround?
Cheers!!
For what is worth, I ended up doing it like this (probably not the best solution...)
\newcommand{\PEwidth}{10}
\newcommand{\PEheight}{5}
\tikzset{
pics/PE/.style
2 args={
code={
\draw (0,0) rectangle (\PEwidth,\PEheight);
\foreach \i in {1,...,#2}
{
\pgfmathsetmacro\x{0.1*\PEwidth + 0.8*\PEwidth*rnd}
\pgfmathsetmacro\y{0.1*\PEheight + 0.8*\PEheight*rnd}
\node[particle] at (\x,\y) (-\i) {};
}
}
}
}
Related
I have two squares. The green one is constrained to the top, and the blue one is constrained between the green one and the bottom of the screen. When clicking on the blue square, the green square doubles in size. Since the size of the green square increases, the blue square moves downwards because of its constraints.
How do I animate the position change of the blue square? Do I have to manually calculate and animate the offset? This feels very hacky. I feel like this should be doable declaratively, since the new position is automatically calculated by the updated constraints.
Edit: Basically, I'm looking for android:animateLayoutChanges, but for Compose.
Here is my code:
#Composable
fun AnimationTest() {
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
val (block1, block2) = createRefs()
var blockSize by remember { mutableStateOf(20.dp) }
Surface(
color = Color.Green,
modifier = Modifier
.size(blockSize)
.constrainAs(block1) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
) {}
Surface(
color = Color.Blue,
modifier = Modifier
.size(50.dp)
.constrainAs(block2) {
top.linkTo(block1.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
}
.clickable {
blockSize *= 2
}
) {}
}
}
Try wrapping it inside the AnimatedContent composable. Though it is ConstraintLayout so I don't think it would work, but it just might. Give it a try and let me know. Otherwise, it is a very simple scenario, and can be easily achieved with the Layout composable, inside which you can use lerp() to achieve the animation
I am trying to draw svg/pointcloud points on individual elements in Autodesk Forge. How to get (x,y,z) coordinates of elements.
Is there a way to extract positions of individual object so that we have the position vector in world space.
What I tried so far:
I cant seem to understand how to use the positions array as described in this thread using
frags = viewer.impl.getRenderProxy( viewer.model, fragId )
positions = frags.geometry.vb
Autodesk.ADN.Viewing.Extension.MeshData.js gives me the vertices of triangles (meshes/fragments) the element is made of.
I've created a self-contained extension that you can add to your viewer and illustrates how to get the component position:
Check this repo and the TransformationExplorerExtension
The code responsible for extracting the transformation matrix is:
getFragmentWorldMatrixByNodeId(nodeId) {
let result = {
fragId: [],
matrix: [],
};
let viewer = this.viewer;
this.tree.enumNodeFragments(nodeId, function (frag) {
let fragProxy = viewer.impl.getFragmentProxy(viewer.model, frag);
let matrix = new THREE.Matrix4();
fragProxy.getWorldMatrix(matrix);
result.fragId.push(frag);
result.matrix.push(matrix);
});
return result;
}
Is it possible to create a bar like this one in Unity?
The default components seem to work primitive only. I can have a rectangle as a bar or a radial bar.
This spiral bar would need to have access to a path because the color has to know how to move along the sprite.
I would use a custom shader with a sprite containing the "fill information" as the alpha channel.
On the image below, you will see your original sprite, and an other one with a gradient alpha (sorry, I'm not an expert with photoshop).
You can download the Unity shaders on their website, and pick the UI-Default.shader inside DefaultResourcesExtra/UI and tweak it a little bit so as to fill the sprite according to the alpha value of the sprite.
Something like this (not tested)
Shader "UI/FillAlpha"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_FillAmount ("Fill amount", Float) = 1
// ...
}
SubShader
{
// ...
sampler2D _MainTex;
fixed _FillAmount;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
color.a = color.a > _FillAmount ? 1 : 0 ;
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
// ...
return color;
}
ENDCG
}
}
}
Then, you will be able to change the FillAmount parameter using myMaterial.SetFloat
You can use Line Renderer component to create your bar.
See documentation and examples here:
https://docs.unity3d.com/Manual/class-LineRenderer.html
https://docs.unity3d.com/352/Documentation/Components/class-LineRenderer.html
Note that Line Renderer works in a 3D space.
Okay, I am almost finished with my 2d rpg click to move game. If you look at this video you will be able to see that when I click forward my player, once it gets to it's position, face the wrong direction rather than facing straight towards the players. To make myself more clearer, this is an image of the sprite sheet I am currently using, as you can see it has 8 directions. When you click here (in the game), my player would walk down and face this direction or this direction, rather than facing this direction (the normal/preferred position). This also happens when I click here (in the game) my player would walk up and face this direction or face this direction, rather than facing this direction. How can I make sure that my player face the right direction once it reached it's destination. Again this is only occurring within the Y-axis, so walking along the X-axis is fine.
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
var movementDirection = (target - transform.position).normalized;
Vector3 animDirection = Vector3.zero;
if (movementDirection.sqrMagnitude > 0)
{
// Use >= to default to horizontal on both being equal
if (movementDirection.x > movementDirection.y)
animDirection.x = 1;
else
animDirection.y = 1;
anim.SetBool ("walking", true);
anim.SetFloat ("SpeedX", movementDirection.x);
anim.SetFloat ("SpeedY", movementDirection.y);
if (movementDirection.x < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else if (movementDirection.x > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else {
anim.SetFloat ("LastMoveX", 0f);
}
if (movementDirection.y > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (movementDirection.y < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
}
} else {
if (Mathf.Approximately (transform.position.x, target.x) && Mathf.Approximately (transform.position.y, target.y)) {
touched = false;
anim.SetBool ("walking", false);
} else {
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
}
}
}
I'm just starting out with Unity, but hope I can help.
From the video you've posted I've noticed that the sprite is only 'wrong' when the oldPosition and newPosition click differs in the X component, e.g. when you would click straight down it would show the desired behavior and sprite.
Have you tried printing out the x and y values that your code is using to calculate which sprite it's setting?
At first I thought that maybe it's because the values you set in the Blend Tree were at -1, 1 etc, and due to normalizing you sometimes wound up with 0.9 for a certain value.
Can you maybe try debugging it with the animator window open, like you did at the end? Writing down the values and comparing them between Desired and Undesired behavior might tell you something more.
Sorry I don't have a concrete solution to your problem, but I hope this helps.
Edit for clarification:
Basically, what I'm recommending is to:
1) Print out the values you are getting when the behavior happens, an example of how to print these things out is by using LogFormat, for example:
Debug.LogFormat("X: {0}, Y: {1}", xPosition, yPosition);
Now, you will get the values printed out when it finishes moving.
2) Write down the values for when the moving finishes with the 'Wrong' sprite, and keep clicking to move until the 'Right' sprite shows up. Write down the values again.
3) Compare each values, and find the differences. Now deduce why the differences are as they are, using the values in conjunction with your blend trees.
4) Once you know why, go back through your code/blend trees and rewrite/fix it to work as you intended it to.
It's a pretty self-explanatory question.
I am curious as to why there is no built-in function to create gradients. The only way I found to "fake it" is to create a series of lines or rectangles each with a unique color calculated with b.lerpColor.
I saw that the InDesign Object Model has of course the gradient class but I don't know how to access it using basiljs.
Maybe if someone could show me? Many thanks.
Check out this reference http://jongware.mit.edu/idcs6js/pc_Gradient.html
And try it like this:
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
var d = b.doc();
var r = b.rect(0, 0, b.width, b.height);
var myGrad = d.gradients.add({
name: "Col " + (parseInt(Math.random() * 10000)),
type: GradientType.linear
});
myGrad.gradientStops[0].properties = {
stopColor: d.colors.item(2),
location: Math.random() * 50
};
myGrad.gradientStops[1].properties = {
stopColor: d.colors.item(4),
location: 50 + Math.random() * 50
};
r.fillColor = myGrad;
// to set the fill of the gradient use the following line
r.gradientFillAngle = 50;//b.random(-180,180);
}
b.go();
The script creates a new gradient swatch every time you run it.
edit:added gradientFillAngle
Take a look here.
gradientFillAngle number r/w The angle of a linear gradient applied to the fill of the Rectangle. (Range: -180 to 180)